• Resolved ZLC

    (@kindnessville)


    I am looking for a way to be able to automatically add someone who submits a Contact Form 7 to a “Group” within an “Audience” in Mailchimp.

    I was helped by someone working for Yikes quite some time ago. He helped me add someone to an “Audience” upon Contact Form 7 submission.

    Here’s the code he came up with to be added to functions.php:

    function cf7_change_MC_list( $list_id ){
        $cf7_form_id = isset( $_POST['_wpcf7'] ) && ! empty( $_POST['_wpcf7'] ) ? filter_var( $_POST['_wpcf7'], FILTER_SANITIZE_NUMBER_INT ) : 0;
        if ( (int) $cf7_form_id === 111111) {
            return "1a11a11a1a"; 
        }
    return $list_id;
    }
    
    add_filter( 'yikes-mailchimp-checkbox-integration-list-id', 'cf7_change_MC_list' );

    It works beautifully when this was included in the contact form:

    [yikes_mailchimp_checkbox]

    I would appreciate a way to drill down to a specific “Group,” if possible.

    Is this something you could please help me with?

    Thank you so much.

Viewing 8 replies - 1 through 8 (of 8 total)
  • Hi @kindnessville,

    We have another filter that allows you to modify the overall body of the request sent to MailChimp. Here is the call as you can see it in our plugin:

    
    $data = apply_filters( 'yikes-mailchimp-checkbox-integration-body', $data, $type, $list_id, $integration_vars );
    

    So if you added a filter on yikes-mailchimp-checkbox-integration-body, you’d receive four parameters. The first one is the actual request data, and that array contains the key interests, which is an array of Interest Group ID’s.

    Ultimately, you’d probably do something like the following:

    function cf7_change_MC_groups( $data, $type, $list_id, $integration_vars ){
        // Get your form id
        $cf7_form_id = isset( $_POST['_wpcf7'] ) && ! empty( $_POST['_wpcf7'] )
                     ? filter_var( $_POST['_wpcf7'], FILTER_SANITIZE_NUMBER_INT )
                     : 0;
    
        // If form id is desired id
        if ( (int) $cf7_form_id === 111111 ) {
            // Add your desired group id
            $data['interests'] = array( 12345 );
        }
    
        // Return the form data
        return $data;
    }
    
    add_filter( 'yikes-mailchimp-checkbox-integration-body', 'cf7_change_MC_groups' );

    Let me know if that helps,
    Jon

    Plugin Contributor Tracy Levesque

    (@liljimmi)

    ?????? YIKES, Inc. Co-Owner

    Hi @kindnessville

    We added groups to our Integrations a few years ago. You should see the groups if you pick a list with groups.

    https://cloudup.com/ihcKpArpOh9

    Thank you.
    -Tracy

    Thread Starter ZLC

    (@kindnessville)

    Thank you, Jon and Tracy.

    I’m trying to make the “add to Mailchimp Group” feature work for multiple, unique contact forms.

    So contact form #1 adds people to Group A.
    Contact form #2 adds people to Group B.
    Etc.

    Jon, with your function above, I’m not sure what variables I need to change in it and to make the contact form add someone to a specific group.

    I see the variable 111111 for the Contact Form 7 ID, but what do I need to change to add it to a certain group?

    That’s the part I need help with.

    Thank you so much.

    Hey @kindnessville,

    In the example code you would change 111111 to the id of the CF7 form, and you’d change 12345 to the id of the group in mailchimp.

    You can simply repeat that if statement with more combinations of CF7 ID / Mailchimp Group ID if you want to support more than one mapping.

    If you have a lot of them, something like this might be cleaner:

    function cf7_change_MC_groups( $data, $type, $list_id, $integration_vars ){
        // Get your form id
        $cf7_form_id = isset( $_POST['_wpcf7'] ) && ! empty( $_POST['_wpcf7'] )
                     ? filter_var( $_POST['_wpcf7'], FILTER_SANITIZE_NUMBER_INT )
                     : 0;
    
        $mapping = array(
            // 'contact_form_7_id' => 'mailchimp_group_id'
            11111 => 11,
            11112 => 12
        );
    
        foreach ($mapping as $key => $val) {
            // If form id is desired id
            if ( (int) $cf7_form_id === $key ) {
                // Add your desired group id
                $data['interests'] = array( $val );
                break;
            }    
        }
    
        // Return the form data
        return $data;
    }
    
    add_filter( 'yikes-mailchimp-checkbox-integration-body', 'cf7_change_MC_groups' );

    Let me know if that helps,
    Jon

    Thread Starter ZLC

    (@kindnessville)

    That’s great, Jon. One more question: Where can I find the ID for the Mailchimp Groups?

    I’m able to find my Audience ID, but not the Groups.

    Thanks so much!

    Thread Starter ZLC

    (@kindnessville)

    Hi Jon (and anyone else attempting to implement this for their contact forms),

    When I activated the original function you shared above in a custom functions plugin, it caused a critical error on my site.

    I was able to remove it, but I don’t know what’s wrong with it to fix it.

    I wonder if you have a solution.

    Also, I was able to get the group ID by right-clicking the group in the Easy Forms integrations settings and inspecting the element. I figured it was one of the two numbers after the audience ID:

    <input type="checkbox" name="optin-checkbox-init[comment_form][interest-groups][audience ID][df9a7df7f6][]" value="3d58f0df09">

    Either df9a7df7f6 or 3d58f0df09

    • This reply was modified 3 years, 8 months ago by ZLC.

    Hi @kindnessville,

    The code itself is valid PHP, so it shouldn’t cause any critical errors as long as you copied everything exactly. That being said, I would recommend testing this locally or on a staging environment first. Are you able to access the debug log to see what was wrong?

    When adding the group ID, it will be a string, so you need to make sure you wrap it in quotation marks. For example, “3d58f0df09” instead of 3d58f0df09.

    Let me know if that helps,
    Jon

    Thread Starter ZLC

    (@kindnessville)

    Ah, quotation marks. Sorry, I’m a big newbie at this, so I’m sure the fact that I didn’t put quotation marks around the group ID is what caused the issue.

    Thank you, Jon. I’ll see if this works now.

    I appreciate your help!

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Groups in Contact Form 7’ is closed to new replies.