Support » Plugin: Brilliant Web-to-Lead for Salesforce » Capture Country Code and IP

  • Hope you are all doing well.
    I am confused as all heck and trying to get a site launched.

    This is what the client asked..
    Can the country code be auto-filled based on their location?
    For the reference code we wanted to it pull their IP address and ideally how they came to the site? Is that set up?

    Here are two fields from his form
    Country Code:<input id=”00N440000066iQx” maxlength=”2″ name=”00N440000066iQx” size=”20″ type=”text” /><br>

    Tracking Reference:<textarea id=”00N440000066iO3″ name=”00N440000066iO3″ rows=”3″ type=”text” wrap=”soft”></textarea><br>

    I can’t for the life of me, figure out how to do this and I am behind on schedule because I can’t get this figured out.

    Thank you in advance for any help you can offer

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

    (@nickciske)

    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.
    Plugin Author Nick Ciske

    (@nickciske)

    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.
    Plugin Author Nick Ciske

    (@nickciske)

    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.
Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Capture Country Code and IP’ is closed to new replies.