Support » Plugin: Airpress » Pass User Information to Airtable

  • Resolved furilla

    (@furilla)


    I have a use case where a user on a wordpress site fills in an Airtable form. I would like to have the user name and/or email address for the logged user in wordpress to be captured in the form. Is there a way to achieve this? Does Airpress handle this?
    Thanks in advance for your help,
    Ed

    The page I need help with: [log in to see the link]

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Author chetmac

    (@chetmac)

    Yes, this is possible and doesn’t require Airpress to work. If you place the code below into your functions.php file you’ll have a shortcode that allows you to embed a form and prefill specific fields

    Airtable Prefill:
    https://support.airtable.com/hc/en-us/articles/234982508-Prefilling-a-form

    WordPress User:
    https://codex.wordpress.org/Function_Reference/wp_get_current_user

    Working Example:
    https://gist.github.com/chetmac/e55899bd14dcf803846ad524c538c248

    
    <?php
    
    // [airtableform form_id="shr1StOpJOAptb7r8" color="blue"]
    function airtableform_func( $atts ) {
    
    	$a = shortcode_atts( array(
    		'form_id' => '',
    		'width' => '100%',
    		'height' => '533',
    		'color' => 'red',
    	), $atts );
    
    	$current_user = wp_get_current_user();
    
    	$prefill_string = "";
    
    	if ( $current_user->exists() ) {
    
    		$prefill = [
    			"Name" => $current_user->user_login,
    			"Notes" => $current_user->user_email
    		];
    
    		foreach($prefill as $key => $value){
    			$prefill_string .= "&prefill_" . rawurlencode($key) . "=" . rawurlencode($value);
    		}
    
    	}
    
    	return '<iframe class="airtable-embed" src="https://airtable.com/embed/' . $a["form_id"] . '?backgroundColor=' . $a["color"] . $prefill_string . '" frameborder="0" onmousewheel="" width="' . $a["width"] . '" height="' . $a["height"] . '" style="background: transparent; border: 1px solid #ccc;"></iframe>';
    
    }
    
    add_shortcode( 'airtableform', 'airtableform_func' );
    
    furilla

    (@furilla)

    Thanks, i think i am getting closer but I am still not able to pass the information i need to the form.

    I have added the following code to the files in WordPress. I must be missing something as I am not getting the prefilled field. Am I suppose to be adding something to the WordPress page to indicate what to prefill?

    WordPress (embedded code from airtable)
    <iframe class=”airtable-bed” style=”background: transparent; border: 1px solid #ccc;” src=”https://airtable.com/embed/shrRMSNCSSaPw8BDG?backgroundColor=blue&#8221; width=”100%” height=”533″ frameborder=”0″></iframe>

    Functions.php (added at end of file)

    <?php
    // [airtableform form_id=”shrRMSNCSSaPw8BDG” color=”blue”]
    function airtableform_func( $atts ) {
    $a = shortcode_atts( array(
    ‘form_id’ => ”,
    ‘width’ => ‘100%’,
    ‘height’ => ‘533’,
    ‘color’ => ‘red’,
    ), $atts );
    $current_user = wp_get_current_user();
    $prefill_string = “”;
    if ( $current_user->exists() ) {
    $prefill = [
    “Name” => $current_user->user_login,
    “user_email” => $current_user->user_email
    ];
    foreach($prefill as $key => $value){
    $prefill_string .= “&prefill_” . rawurlencode($key) . “=” . rawurlencode($value);
    }
    }
    return ‘<iframe class=”airtable-embed” src=”https://airtable.com/embed/&#8217; . $a[“form_id”] . ‘?backgroundColor=’ . $a[“color”] . $prefill_string . ‘” frameborder=”0″ onmousewheel=”” width=”‘ . $a[“width”] . ‘” height=”‘ . $a[“height”] . ‘” style=”background: transparent; border: 1px solid #ccc;”></iframe>’;
    }
    add_shortcode( ‘airtableform’, ‘airtableform_func’ );

    Plugin Author chetmac

    (@chetmac)

    You must be logged into WordPress, otherwise the values in the $prefill array won’t be populated.

    As for where you define what you want populated, that is in the $prefill array. You can add anything you want there. The key (the string on the left side of the equals sign) must correspond to the Airtable field name.

    If you do “view source” on the page, do you see something like this?

    <iframe class="airtable-embed" src="https://airtable.com/embed/shrRMSNCSSaPw8BDG?backgroundColor=blue&prefill_Name=chester&prefill_user_email=your@email.address" frameborder="0" onmousewheel="" width="100%" height="533" style="background: transparent; border: 1px solid #ccc;"></iframe>

    To further narrow it down you can replace $current_user->exists() with simply true and then “hard code” some value in the Name prefill to see if that works.

    furilla

    (@furilla)

    That worked perfectly. Thanks so much.

    I don’t want to put in a fixed name and email address. Can I prefill the current user display name. If so, What is the syntax for doing that.

    I want to Capture the current wordpress user display name and email address.

    • This reply was modified 5 months ago by furilla.
    • This reply was modified 5 months ago by furilla.
    furilla

    (@furilla)

    • This reply was modified 5 months ago by furilla.
    furilla

    (@furilla)

    Regarding Chester’s outstanding support: I would like to say that Chester could not have been more helpful in getting me through this problem. Working with him was nothing short of excellent.

    Thanks very much,
    Ed

Viewing 6 replies - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.