From the Hooks & Filters section of the README:
// Autofill a user's country based on IP
add_filter( 'salesforce_w2l_field_value', 'salesforce_w2l_field_value_geoip_example', 10, 3 );
function salesforce_w2l_field_value_geoip_example( $val, $field, $form ){
// Based on this plugin: https://wordpress.org/plugins/geoip-detect/
// Adjust this code to the one used by your geo detection plugin as needed
if( !function_exists( 'geoip_detect2_get_info_from_current_ip' ) ) return;
$form_id = 1; // form id to act upon
$field_name = '00N440000066iQx'; // API Name of the field you want to autofill
if( $form == $form_id && $field_name == $field ){
$userInfo = geoip_detect2_get_info_from_current_ip();
//$val = $userInfo->country->isoCode; // e.g. US
$val = $userInfo->country->name; // e.g. United States
}
return $val;
}
Getting the IP address (same idea/filter as above, different field):
https://www.virendrachandak.com/techtalk/getting-real-client-ip-address-in-php-2/
The “ideally how they came to the site” request is trickier, an external referer is only available if they go directly to your form from somewhere offsite. Otherwise, you need to track that in a cookie, or grab it from Google Analytics (if you’re using that).
From the Hooks & Filters section of the README:
// Store HTTP referrer in a field (this is not 100% reliable as the browser sends this value to the server)
add_filter( 'salesforce_w2l_field_value', 'salesforce_w2l_field_value_referrer_example', 10, 3 );
function salesforce_w2l_field_value_referrer_example( $val, $field, $form ){
$form_id = 1; // form id to act upon
$field_name = 'referrer__c'; // API Name of the field you want to autofill
if( $form == $form_id && $field_name == $field ){
if( isset( $_SERVER['HTTP_REFERER'] ) ){
return $_SERVER['HTTP_REFERER'];
}
}
return $val;
}
Good luck. if you need further assistance, can I suggest premium support?
https://luminfire.com/support/premium-plugin-support/
Thank you for your reply. I am going to give this all a shot. Fingers crossed, as that IP one kinda has my head spinning LOL
Well, I managed to get the Country to autofill in the one form.
When I try to set it up so that it autofills in both forms, I get an error because I cannot duplicate the code in the functions.
I have a contact form and a form for requesting a demo, can I only have it autofill for one of the forms?
-
This reply was modified 3 years, 7 months ago by
teclive.
Yes, you can target specific form & field by appending the form & field ID to the end of the hook name.
e.g.
salesforce_w2l_field_value
=> salesforce_w2l_field_value_{Form ID}_{Field Name}
So this would target the country code field on form 12:
salesforce_w2l_field_value_12_00N440000066iQx
If you go that route you can dispense with the form and field checking in the functions as the hook only gets called once.
This is mine now for the demo request
$form_id = 8; // form id to act upon
$field_name = ’00N440000066iO3′; // API Name of the field you want to autofill
My full code is
// Autofill a user's country based on IP DEMO
add_filter( 'salesforce_w2l_field_value', 'salesforce_w2l_field_value_geoip_example', 10, 3 );
function salesforce_w2l_field_value_geoip_example( $val, $field, $form ){
// Based on this plugin: https://wordpress.org/plugins/geoip-detect/
// Adjust this code to the one used by your geo detection plugin as needed
if( !function_exists( 'geoip_detect2_get_info_from_current_ip' ) ) return;
$form_id = 8; // form id to act upon
$field_name = '00N440000066iO3'; // API Name of the field you want to autofill
if( $form == $form_id && $field_name == $field ){
$userInfo = geoip_detect2_get_info_from_current_ip();
//$val = $userInfo->country->isoCode; // e.g. US
$val = $userInfo->country->name; // e.g. United States
}
return $val;
}
but when I try to duplicate that code and change the formID and field name for the other form, it tells me it has already been sent
-
This reply was modified 3 years, 7 months ago by
teclive.
If you duplicate a function, you need to give it a unique name (in the declaration and make sure your hook references the same function name).
-
This reply was modified 3 years, 6 months ago by
Nick Ciske.