• Resolved golightlygl

    (@golightlygl)


    I’ve seen the previous replies showing how to do this by pointing to the ibericode code snippet.

    add_filter('mc4wp_integration_wpforms_data', function($data) {
        $data['FNAME'] = $_POST['wpforms']['fields'][4];
        return $data;
    });

    However, without documentation this is a little difficult to understand for amateur coders so please could you bear with me and answer a couple of questions.

    The inspect element on my WP-Forms form shows:

    <select id="wpforms-2672-field_6" class="wpforms-field-medium wpforms-field-required wpforms-valid" name="wpforms[fields][6]" required="required" aria-invalid="false"><option value="United States">United States</option><option value="United Kingdom">United Kingdom...</select>

    am I right in thinking that in the code snippet I would use wpforms[fields][6] instead of [‘wpforms’][‘fields’][4]

    Please could you be precise in answering with regard to brackets and quotes etc.

    And I just change the FNAME in the code snippet to the MC tag (in my case COUNTRY?

    Thanks in advance for your help

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

Viewing 10 replies - 1 through 10 (of 10 total)
  • Plugin Contributor Harish Chouhan

    (@hchouhan)

    Hey @golightlygl,

    Thanks for reaching out to us.

    First, please copy our code, and then the only thing you need to replace is, “FNAME” with the merge field name in MailChimp and the number “4” with 6″.

    Regarding the merge field name, you can get that field name by first using our form and then adding that field to the form. This will then show you the correct field name.

    Thread Starter golightlygl

    (@golightlygl)

    Thank you, that is clear. However, I want to be confident I’m not going to destroy anything in the way I use the code. So to be clear:

    1) Locate: WP-Includes/functions.php
    2) Scroll to the bottom of the file
    3) add the code as a function: ie the actual code added would be

    function add_filter('mc4wp_integration_wpforms_data', function($data) {
        $data['COUNTRY'] = $_POST['wpforms']['fields'][6];
        return $data;
    }

    so I have added the word function to your code snippet and deleted the final bracket and semi-colon );

    The field name COUNTRY was found in the Mailchimp, Audience, Settings page under the column Put this tag in your content:

    The field ID 6 was found by inspecting the element in a browser when the field is displayed

    Thank you for your patience helping a non-coder through this

    Plugin Contributor Lap

    (@lapzor)

    It would be in your theme’s functions.php file, not in the file you referenced.

    If you don’t have experience doing this, I recommend to use the Code Snippets plugin instead of editing the files:

    https://wordpress.org/plugins/code-snippets/

    That way, if you make some mistake you can delete the code snippets plugin folder from wp-content/plugins to get your site back online, because if you do make even a small spelling mistake in this code it can indeed take your site offline!! For example removing a semicolon will break the code. That final bracket and semicolon is needed for the code to work.

    Always make a good full backup of your site before making any code changes.

    Hope that helps. If you have any questions, please let me know!

    Thread Starter golightlygl

    (@golightlygl)

    Well that’s a massive help: thank you! Definitely the way to go for we non-coders.

    And I can report that I’ve got it to work so that’s great. However, I am having a problem with WPFORMS Name field.

    This is a field that has two halves (FNAME, LNAME) and I thus can’t identify the field IDs to use in the code snippet. It seems to be identified as Field 0 First and Field 0 Last (shown below) when I inspect the element.

    <input type="text" id="wpforms-2672-field_0-last" class="wpforms-field-name-last wpforms-field-required" name="wpforms[fields][0][last]" required="">

    SO would I replicate that in the code snippet thus:

    add_filter(‘mc4wp_integration_wpforms_data’, function($data) {
    $data[‘FNAME’] = $_POST[‘wpforms’][‘fields’][0][first];
    return $data;
    });

    As a secondary question: if I have mutiple fields that I want to add, do I have multiple code snippets or can I add them all as one large snippet encompassing all the fields (and if so how).

    Thanks again. I’m really not asking without trying and learning, but I am a tad lost.

    Plugin Contributor Lap

    (@lapzor)

    Did you already try $_POST[‘wpforms’][‘fields’][0][first] (the way you show above)?

    That seems to me to be the correct way of doing it.

    You can add them all in 1 code snippet

    function add_filter('mc4wp_integration_wpforms_data', function($data) {
        $data['field1'] = $_POST['wpforms']['fields'][6];
        $data['field2'] = $_POST['wpforms']['fields'][7];
        $data['field3'] = $_POST['wpforms']['fields'][8];
        return $data;
    });

    Hope that helps. If you have any questions, please let me know!

    Thread Starter golightlygl

    (@golightlygl)

    So $_POST[‘wpforms’][‘fields’][0][first] worked. Thanks.

    But that is the only additional field geting returned from my multi-field snippet. I added:

    add_filter('mc4wp_integration_wpforms_data', function($data) {
        $data['FNAME'] = $_POST['wpforms']['fields'][0][first];
        return $data;
    	$data['LNAME'] = $_POST['wpforms']['fields'][0][last];
        return $data;
    	$data['COUNTRY'] = $_POST['wpforms']['fields'][6];
        return $data;
    	$data['GDPR'] = $_POST['wpforms']['fields'][7];
        return $data;
    	$data['OPTIN'] = $_POST['wpforms']['fields'][3];
        return $data;
    });

    in the Edit Snippet plugin but only the first field gets returned. There is no error in the MC4WP console and no syntax warnings on the edit snippet page. If I change the order of the fields, whatever is the first field listed in the snippet gets returned but none of the others.

    Plugin Contributor Harish Chouhan

    (@hchouhan)

    Hey @golightlygl,

    Please delete all instances of the code “return $data;” except the last line and test again.

    Thread Starter golightlygl

    (@golightlygl)

    That was it. Perfect. Thank you both for your help: I’m good to go now.

    Hello,

    I also struggling with this.
    I have a “first name” (single field) email and checkbox. The output HTML is as shown here : (all unecessary code stripped)

    <input type="text" id="wpforms-780-field_7" class="wpforms-field-small wpforms-field-required" name="wpforms[fields][7]" placeholder="Prénom" required>
    <input type="email" id="wpforms-780-field_1" class="wpforms-field-small wpforms-field-required" name="wpforms[fields][1]" placeholder="email@email.com" required>
    

    In a custom plugin (where I put all my logic, instead of functions.php, I have :

    add_filter('mc4wp_integration_wpforms_data', function($data) {
        $data['First Name'] = $_POST['wpforms']['fields'][7];
        return $data;
    });
    

    The email is saved, the ‘First Name’ field never, though “First Name” does exist in my audience…

    Please help, I am banging my head on the keyboard here…

    Plugin Contributor Lap

    (@lapzor)

    $data[‘First Name’] seems incorrect to me. Please fill the MERGE TAG of the field instad of First Name. Often this would be

    $data[‘FNAME’]

    Hope that helps. If you have any questions, please let me know!

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘WP-Forms Lite custom field integration’ is closed to new replies.