• Resolved alexbosch

    (@alexbosch)


    Hi;

    I’m using your plugin to integrate Mailchimp with WPforms. I found on your snippets that it is possible to integrate different tags for every form on Contact Form 7using custom functions.php code.

    Is it possible with WPforms? I tried changing your CF7 snippet, but I couldn’t find the way to make it work.

    Thank you in advance.

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

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

    (@hchouhan)

    Hey @alexbosch,

    We do not have a ready code for this.

    Please find an example code below.

    The filters you can for integrations are:

    mc4wp_integration_affiliatewp_subscriber_data
    mc4wp_integration_buddypress_subscriber_data
    mc4wp_integration_contact-form-7_subscriber_data
    mc4wp_integration_custom_subscriber_data
    mc4wp_integration_easy-digital-downloads_subscriber_data
    mc4wp_integration_events-manager_subscriber_data
    mc4wp_integration_give_subscriber_data
    mc4wp_integration_gravity-forms_subscriber_data
    mc4wp_integration_memberpress_subscriber_data
    mc4wp_integration_ninja-forms-2_subscriber_data
    mc4wp_integration_ninja-forms_subscriber_data
    mc4wp_integration_woocommerce_subscriber_data
    mc4wp_integration_wp-comment-form_subscriber_data
    mc4wp_integration_wp-registration-form_subscriber_data
    mc4wp_integration_wpforms_subscriber_data

    Here is an example code for WooCommerce:

    add_filter( 'mc4wp_integration_woocommerce_subscriber_data', 'myprefix_woocommerce_subscriber_data' );
    
    function myprefix_woocommerce_subscriber_data( MC4WP_MailChimp_Subscriber $subscriber ) {
    
    $subscriber->tags[] = 'My tag';
       return $subscriber;
    }

    You just need to replicate the code, change the function name, and the filter name based on different integrations. You will also need some additional logic to check which WPForm it is.

    Thread Starter alexbosch

    (@alexbosch)

    Hi @hchouhan

    Thank you for your rapid response.

    I already found your integration filters on your Github page. With them I managed to send a the same tag for all my WPforms with this code:

    add_filter( ‘mc4wp_integration_wpforms_subscriber_data’, function(MC4WP_MailChimp_Subscriber $subscriber) {
    $subscriber->tags[] = ‘ES’;
    return $subscriber;
    });

    But I had problems when I tried to adapt this code from Contact Forms to WPforms:

    if ($cf7_form_id == 500) {
           $subscriber->tags[] = 'ES';
       } else if ($cf7_form_id == 510) {
           $subscriber->tags[] = 'NL';
       }

    Which identifier I have to use instead of “$cf_7_form_id”?

    Thank you in advance.

    Plugin Contributor Harish Chouhan

    (@hchouhan)

    Hey @alexbosch,

    I would recommend contacting WPForm’s support as they would be able to provide some details on how to get the form ID using PHP code.

    Thread Starter alexbosch

    (@alexbosch)

    Hi @hchouhan

    We already found how to adapt the code to wpforms. This will be an example code:

    add_filter( ‘mc4wp_integration_wpforms_subscriber_data’, function(MC4WP_MailChimp_Subscriber $subscriber) {
    $form_id = $_POST[‘wpforms’][‘id’];
    if ($form_id == 500) {
    $subscriber->tags[] = ‘ES’;
    } else if ($form_id == 510) {
    $subscriber->tags[] = ‘EN’;
    }
    return $subscriber;
    }, 10, 2);`

    Now, I have another question: If I want to assign one tag to various forms, I need to replicate the entire line, like this:

    if ($form_id == 500) {
    $subscriber->tags[] = ‘ES’;
    } else if ($form_id == 502) {
    $subscriber->tags[] = ‘ES’;
    }

    Or, is there a way to do it without repeating the code?

    At he same time, is there a way to assign more than one tag per form without repeating the entire line of code?

    if ($form_id == 500) {
       $subscriber->tags[] = 'ES';
    } 
    if ($form_id == 502) {
       $subscriber->tags[] = 'CA';
    }

    Thank you in advance!`

    Plugin Contributor Lap

    (@lapzor)

    You could use a switch: https://www.php.net/manual/en/control-structures.switch.php

    $subscriber->tags is an array. adding the [] will add 1 array item. With array_push you can add multiple: https://www.php.net/manual/en/function.array-push.php

    Or you could create an array with the language per form id.

    https://www.php.net/manual/en/language.types.array.php

    $id_to_country = array(
        500  => 'ES',
        502 => 'CA',
        key3 => value3,
        ...
    )
    $subscriber->tags[] = $id_to_country[$form_id];
    

    And I’m sure there is other creative solutions possible,…

    By the way, since you’re taking data from $_POST (so user submitted data) you should sanitize that! In this case you could simply use is_nummeric to make sure only numbers can come through.

    $form_id = (is_numeric($_POST[‘wpforms’][‘id’])) ? $_POST[‘wpforms’][‘id’]: ”;
    or in other words
    if(is_numeric($_POST[‘wpforms’][‘id’])) { $form_id = $_POST[‘wpforms’][‘id’]; }

    Note that officially helping you write custom PHP code is far out of scope for our free support. I hope I’ve put you on the right path with this information so you can figure it out for yourself from here. For further help with this I recommend you hire a PHP coder.

    Cheers!

    Thread Starter alexbosch

    (@alexbosch)

    Hi @lapzor

    I understand you. I’m very satisfied with your support.

    I’ll try what you said.

    Thank you again!

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Add different tags to every wpforms form’ is closed to new replies.