• Resolved mgiles

    (@mgiles)


    I want to pass a query parameter from the URL (e.g. ?source=onf) into a hidden field on my form so that it can be stored in SalesForce alongside the other detail in the form. What is the best way to go about that?

    I’ve seen mentions of bits and pieces of the solution to this (first with JQuery, then with filters) but not enough for me to put together a working solution. So a sample of some code (and where that code would live) would be great (I’m not a WordPress expert so saying “use a filter” isn’t terribly helpful).

    -Mike

    https://wordpress.org/plugins/salesforce-wordpress-to-lead/

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

    (@nickciske)

    Use a filter… and this code:

    add_filter( 'salesforce_w2l_field_value', 'salesforce_w2l_field_value_querystring_example', 10, 3 );
    
    function salesforce_w2l_field_value_querystring_example( $val, $field, $form ){
    
    	$form_id = 1; // form id to act upon
    	$field_name = 'source__c'; // API Name of the field your want to autofill
    	$qs_var = 'source'; // e.g. ?source=foo
    
    	if( $form == $form_id && $field_name == $field ){
    		if( isset( $_GET[ $qs_var ] ) ){
    			return $_GET[ $qs_var ];
    		}
    	}
    
    	return $val;
    
    }

    Put that in functions.php or use pluginception to create a plugin and put it there.

    Change the form, field and querystring variable names as needed.

    jpiper.kmi

    (@jpiperkmi)

    I believe I found a bug in which this script seems to erase the values out of all the other hidden fields in the form. I’m trying to both set a hidden field value with the query-string and also hard code a different hidden field with a value in the basic form editor.

    Any ideas?

    Plugin Author Nick Ciske

    (@nickciske)

    I believe I found a bug in which this script seems to erase the values out of all the other hidden fields in the form.

    This script, or a modified one? There’s several levels of checks to insure it only affects 1 field on 1 form… and only if the qs var is set.

    If you’ve modified it… then the bug is likely in your code.

    Feel free to provide the code for review…

    jpiper.kmi

    (@jpiperkmi)

    Thanks for the quick reply! I’m certainly not to proud to admit I might have a bug. (See below for code.) I customized this in hopes to apply it to any form on the site, but only for the specific fields listed as variables.

    add_filter( 'salesforce_w2l_field_value', 'salesforce_w2l_field_value_querystring_example', 10, 3 );
    function salesforce_w2l_field_value_querystring_example( $val, $field, $form ){
    
    	// API Name of the field your want to autofill
    	$field_name_source = 'SEO_Source__c';
    	$field_name_medium = 'SEO_Medium__c';
    	$field_name_campaign = 'SEO_Campaign__c';
    	$field_name_content = 'SEO_Content__c';
    	$field_name_term = 'SEO_Term__c';
    
    	$qs_var_source = 'utm_source'; // e.g. ?utm_source=google
    	$qs_var_medium = 'utm_medium'; // e.g. ?utm_medium=ppc
    	$qs_var_campaign = 'utm_campaign'; // e.g. ?utm_campaign=brand
    	$qs_var_content = 'utm_content'; // e.g. ?utm_content=topCTA
    	$qs_var_term = 'utm_term'; // e.g. ?utm_term=foo
    
    	if( $field_name_source == $field ){
    		if( isset( $_GET[ $qs_var_source ] ) ){
    			return $_GET[ $qs_var_source ];
    		}
    		else {};
    	}
    	if( $field_name_medium == $field ){
    		if( isset( $_GET[ $qs_var_medium ] ) ){
    			return $_GET[ $qs_var_medium ];
    		}
    		else {};
    	}
    	if( $field_name_campaign == $field ){
    		if( isset( $_GET[ $qs_var_campaign ] ) ){
    			return $_GET[ $qs_var_campaign ];
    		}
    		else {};
    	}
    	if( $field_name_content == $field ){
    		if( isset( $_GET[ $qs_var_content ] ) ){
    			return $_GET[ $qs_var_content ];
    		}
    		else {};
    	}
    	if( $field_name_term == $field ){
    		if( isset( $_GET[ $qs_var_term ] ) ){
    			return $_GET[ $qs_var_term ];
    		}
    		else {};
    	}
    	return $vals;
    
    }
    Plugin Author Nick Ciske

    (@nickciske)

    return $vals; => return $val;

    jpiper.kmi

    (@jpiperkmi)

    Thanks for the suggestion. I believe I’ve got it working.
    (see updated code below)

    add_filter( 'salesforce_w2l_field_value', 'salesforce_w2l_field_value_querystring_insert', 10, 3 );
    function salesforce_w2l_field_value_querystring_insert( $val, $field, $form ){
    
    	// API Name of the field your want to autofill
    	$field_name_source = 'SEO_Source__c';
    	$field_name_medium = 'SEO_Medium__c';
    	$field_name_campaign = 'SEO_Campaign__c';
    	$field_name_content = 'SEO_Content__c';
    	$field_name_term = 'SEO_Term__c';
    
    	$qs_var_source = 'utm_source'; // e.g. ?utm_source=google
    	$qs_var_medium = 'utm_medium'; // e.g. ?utm_medium=ppc
    	$qs_var_campaign = 'utm_campaign'; // e.g. ?utm_campaign=brand
    	$qs_var_content = 'utm_content'; // e.g. ?utm_content=topCTA
    	$qs_var_term = 'utm_term'; // e.g. ?utm_term=foo
    
    	if( $field_name_source == $field ){
    		if( isset( $_GET[ $qs_var_source ] ) ){
    			return $_GET[ $qs_var_source ];
    		}
    		else {};
    	}
    	if( $field_name_medium == $field ){
    		if( isset( $_GET[ $qs_var_medium ] ) ){
    			return $_GET[ $qs_var_medium ];
    		}
    		else {};
    	}
    	if( $field_name_campaign == $field ){
    		if( isset( $_GET[ $qs_var_campaign ] ) ){
    			return $_GET[ $qs_var_campaign ];
    		}
    		else {};
    	}
    	if( $field_name_content == $field ){
    		if( isset( $_GET[ $qs_var_content ] ) ){
    			return $_GET[ $qs_var_content ];
    		}
    		else {};
    	}
    	if( $field_name_term == $field ){
    		if( isset( $_GET[ $qs_var_term ] ) ){
    			return $_GET[ $qs_var_term ];
    		}
    		else {};
    	}
    	if ($vals >= $val){return $vals;} else{return $val;};
    
    }
Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘Pass query param to field value’ is closed to new replies.