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.
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?
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…
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;
}
return $vals; => return $val;
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;};
}