Add Users to Mailchimp Interest Groups Based on Contact Form 7 Selection

Add a contact to a Mailchimp group depending on the choice in a contact form

Almost every site has a contact form, and often its contacts have to go to a Mailchimp audience. But what if the contact has to be added to a specific Mailchimp group, depending on what the user selected in the form? It is possible, and without custom API code.

Problem: Add the email from a Contact Form 7 form to a Mailchimp list, in the interest group that matches the topic the user selected.

Solution: Use the plugins Contact Form 7 and MC4WP: Mailchimp for WordPress. The plugin has an example of a group choice in CF7 forms, and we will do it a little differently.

The example from the plugin documentation is a select where the user chooses the group directly:

<select name="mc4wp-INTERESTS[ec14a686c8]">
    <option value="5527b34480">Interest group 1</option>
    <option value="462b888077">Interest group 2</option>
</select>

The key in mc4wp-INTERESTS[...] is the ID of the interest category (the group title in Mailchimp), and each option value is the ID of one group inside it.

Where to find the IDs

Way 1. In MC4WP → Mailchimp, click on the list: the plugin shows the list ID, its fields and the interest categories with the IDs of their groups.

Way 2. Through the Mailchimp API (an API key is in Account → Extras → API keys): request /3.0/lists, then /3.0/lists/{list_id}/interest-categories and /3.0/lists/{list_id}/interest-categories/{category_id}/interests. In my case:

  • list_id db58q3b12e is one for the whole form;
  • category_id 34cbb1e321 is the name of the select;
  • id 1b9c2203aa is the group the user must be added to.

The form

If the user chose the group directly, the plain select would be enough:

<select name="mc4wp-INTERESTS[34cbb1e321]">
    <option value="0"></option>
    <option value="1b9c2203aa">Sales Enquiries</option>
    <option value="29cf33340b">Product Information</option>
    <option value="39de449d5c">Partnerships</option>
    <option value="423655ba6d">Career Opportunities</option>
    <option value="53f46673ee">Developer Program</option>
    <option value="6ca777b4ef">Other</option>
</select>

In my case the select is a normal form field with readable values: they go to the email and to a Mailchimp merge field (mc4wp-TEXT2). So the group is passed in a separate hidden field. Add to the Form tab of the Contact Form 7 form:

[select* mc4wp-TEXT2 "Sales Enquiries" "Product Information" "Partnerships" "Career Opportunities" "Developer Program" "Other"]

<input name="mc4wp-INTERESTS[34cbb1e321][]" type="hidden" value="" />
<input name="_mc4wp_lists[]" type="hidden" value="db58q3b12e" />

The second hidden field is the list ID, so the plugin knows which list to use.

Fill the hidden field with jQuery

jQuery( function ( $ ) {
    var groups = {
        'Sales Enquiries':      '1b9c2203aa',
        'Product Information':  '29cf33340b',
        'Partnerships':         '39de449d5c',
        'Career Opportunities': '423655ba6d',
        'Developer Program':    '53f46673ee',
        'Other':                '6ca777b4ef'
    };

    $( 'select[name="mc4wp-TEXT2"]' ).on( 'change', function () {
        $( 'input[name="mc4wp-INTERESTS[34cbb1e321][]"]' ).val( groups[ this.value ] || groups['Other'] );
    } );
} );

When the form is sent, MC4WP reads the fields with the mc4wp- prefix and adds the contact to the selected group.

NOTE: The Contact Form 7 integration has to be enabled in MC4WP → Integrations. Without a [mc4wp_checkbox] in the form, enable “Implicit” there, otherwise nothing is sent to Mailchimp.