A MailChimp Opt-in Field for Contact Form 7

MailChimp is one bad-ass email marketing and email list management tool.

Contact form 7 is also one of the most popular free contact forms for WordPress. Wouldn’t it be nice if they worked together?

With a bit of PHP magic, here’s how to create a MailChimp opt-in field for Contact Form 7 which automatically adds an email address to a MailChimp list.

Create your MailChimp List

Login to your MailChimp account and either select an existing list or create a new one.

Get Your MailChimp API Key

To get your MailChimp API key, from the main menu select Account > API Keys & Authorized Apps.

Store this key somewhere temporary such as Notepad.  You’ll need it later.

Create Your Contact Form

Create whatever type of contact form you like but add the following field somewhere on your form.

This adds a text field with ID your-name for the persons name, a text field with ID your-email for the email address and a checkbox with the ID of mailchimp-optin to the form.

Note: there are spaces in between the square brackets in the code snippet.  You’ll have to remove those if coping directly to the form editor.  I had to add them to prevent WordPress from parsing them as shortcodes 😛

Here’s our General Contact form with opt in field and some other formatting.


Download API File

You will need to download the MailChimp API code from here and upload it to your themes folder.

I suggest you create a folder called includes inside your theme folder and place the MCAPI.class.php file in there.

Note: the file has been zipped up for convenience.  You’ll have to unzip it first on your local computer.


Add Code to functions.php

Once you have uploaded the MCAPI.class.php file to your themes’ includes folder open your functions.php file and add the following code.

The code has comments throughout to guide you so I’ll just summarise here.

Lines 7-15 check to see if the checkbox mailchimp-optin has been checked.

If it has, it then grabs the data entered into the name field your-name and splits it apart using a space to get the first and last name (ignoring any middle names).

Lines 17-21 adds the information to the variables needed by the MailChimp API, including the email address from the your-email form field.

Line 23 loads in the MailChimp API that we uploaded earlier.

Lines 25-26 Replace YOURAPIKEY with your MailChimp API Key which can be found at http://admin.mailchimp.com/account/api/

You have to be logged in first!

Lines 28-30 Replace YOURLISTID with your MailChimp List ID – the one you want the name to be added to.

You can find your lists at http://admin.mailchimp.com/lists/

View the list you want to add the information onto, then click on Settings and the Unique List ID is at the bottom of that page.


Test Your Form

The last thing to do is to test your form.


Enter some information then switch over to your MailChimp list, refresh and you’ll see a new subscriber.

Note: it may take a minute or two for the list to refresh.

Congratulations!  You’re now on your way to building an email marketing list.

Was this article helpful?

Keep In Touch


Wil is a dad, WordPress consultant, WordPress developer, business coach and mentor. He co-organizes the WordPress Sydney meetup group and has been on the organising committee for WordCamp Sydney since 2014. He speaks at many special events and contributes to the WordPress open source project. His likes are chillies, craft beer and electrogravitics.

43 thoughts on “A MailChimp Opt-in Field for Contact Form 7”

  1. Hiya I like your the above code and it seems to really work well, but just for one mailchimp list. When I want to give people the option to subscribe to two different email lists it only subscribe to the latter one. Which seems rather normal if you think of it… I added the following code:

    if ( $formdata[‘mailchimp-optin’] ) {
    $list_id = ‘XXXXXX;
    if ( $formdata[‘mailchimp-optin2’] ) {
    $list_id = ‘YYYYY’;

    The form itself is working etc. but the function is only called once and hence I only get subscribed to the last option. Do you know a way I can change it so that I can subscribe to multiple forms? Thanks in advance again also for helping with the above 🙂

    All the best!

    1. My bad, of course this is not working…created a second variable and did and “isset” if that one is set I fire up the code to push the information to MailChimp 🙂

      1. @jcvangent:disqus – I also need 2 lists, could you explain how you did it please? An example of your code including the 2nd variable would be awesome, thank you.

        1. The way I set it up was using WPML (for the multi language) and I had two list where people could subscribe to (so actually even four lists).

          In my contact form I put a hidden field like this: [hidden language “EN”] to differentiate the languages. And a checkbox for which list they wanted to subscribe to like this: [checkbox list1 …] and [checkbox list2…].

          Than in the function “wpcf7_send_to_mailchimp” from this site I added the following in the spot where you fill in your list id:

          // grab your List’s Unique Id by going to http://admin.mailchimp.com/lists/
          // Click the “settings” link for the list – the Unique Id is at the bottom of that page.

          if ($formdata[‘language’] == ‘EN’) {
          if ( $formdata[‘list1’] ) {
          $list_id = ‘_LIST_ID1_’;
          if ( $formdata[‘list2’] ) {
          $list_id2 = ‘_LIST_ID2_’;
          } elseif ($formdata[‘language’] == ‘_OTHER_LANGUAGE’) {
          // repeat for the correct lists again

          and than later on add this line:
          if(isset($list_id2)) { $retval = $api->listSubscribe($list_id2, $send_this_email, $mergeVars, ‘html’, false,true);}

          to also let people subscribe to the second list.

          Hope this helps!

          1. @jcvangent:disqus Thanks buddy, appreciate your swift response. I’ll look into the code, but the issue I face is I have two CF7 forms and need to target subscriptions to a specific list according to which form is being used. Unfortunately, the form ID’s are tied to the page ID e.g. “… it would be OK to target the page ID i guess, but this is not very future proofed (if the page id changes – i.e. the form gets moved). I’m working on it and will post an update here for the benefit of others.

          2. you don’t have to use the form ID’s for these, I created a hidden field in the form itself and had it running on two different pages to. Than in the function, you just look for the hidden field and base your action around that. Multiple pages with multiple forms and multiple lists to select 😉

  2. If doing this will MailChimp send an email telling the user they were subscribed? If they do, can we personalize that email?

    1. Hi Chloé Adelia Watts.
      How you are built up this intgeration with Contact form that it works.I have tried many times and tested many ways but in vain.Please can you suggest me something?

  3. Thank you for this great code.

    It works.

    I check the checkbox and in my mailchimp list is a new subscriber but the subscriber don’t get a Opt-In Confirmation and not a final welcome-mail? Do you have an idea why?


    1. Hi Susan,

      Look at the function on line 29 listSubscribe. Change the 5th option to ‘true’.

      This is the double-opt-in parameter.

      Open up the MCAPI class and you’ll be able to see the other function parameters around line 1594

      function listSubscribe($id, $email_address, $merge_vars=NULL, $email_type=’html’, $double_optin=true, $update_existing=false, $replace_interests=true, $send_welcome=false)

      Set them for your particular set up.


  4. Hi Wil, I’ve recently discovered that when people only put their first name, and not both first & last, that the email is sent to me that they subscribed, but they are not added to mailchimp. I figured the solution would be to separate the ‘first name’ and ‘last name’ into two separate fields – how would I change the code in functions.php to do that?

  5. Vincent Stephens

    Amazingly good stuff, thanks.

    I would like to add extra field to be added to the mailchimp list. So when submitting the form, the entry Website is added to the list.

    Not really sure how to get this data into the list, any help please. I have tried this:

    function wpcf7_send_to_mailchimp($cfdata) {
    $formtitle = $cfdata->title;
    $formdata = $cfdata->posted_data;
    $website = $formdata[‘website’];
    // Opt-in field checked?
    if ( $formdata[‘mailchimp-optin’] ) {
    $names = explode(‘ ‘,trim($formdata[‘first-name’]));
    $firstName = $names[0];
    $lastName = ”;
    if (count($names)>1){
    // more than one word in name field
    $lastName = array_pop($names);
    $send_this_email = $formdata[‘your-email’];
    $mergeVars = array(

    Having created a mergetag in the list called (WEBSITE) and a form field called [text language=”website”][/text].

    cant see what I am doing wrong here. (although I am more of a jquery guy than php!)
    I am also using two form field for first name and last name so no need to split the value, I haven’t made that change yet.

    You can see the form in action dev.system2.co.uk

  6. Hi Wil,

    Came across this article by Google. Just wanted to let you know that another way to add a sign-up checkbox to your Contact Form 7 forms is by using my plugin called MailChimp for WordPress.

    It uses the WordPress HTTP API so it will work on practically all servers, even without cURL enabled (which the MCAPI class uses). Might be of use for you (or your clients). 🙂

    1. francesca violante vitulano

      But with mail chimp for word press could I add just a check box under my contact form?
      thank you

  7. Using this, taking various forms, each of which must send data to a list of different MailChimp?

  8. francesca violante vitulano

    Hi Wil, Thank you for the guide! Useful! I have a question. I just need to add the checkbox at the contact (not the “email box” or “your name box” Because I have it already in the contact form!). In the function.php what will change?
    Thank you so much!

  9. This is excellent. I haven’t tried it yet, but I can think of a few clients that definitely may want to use it in the future. I’m in Canada, so double opt-in is now what we ‘must’ do here after the newly implemented anti-spam law. Thank you!

  10. Hi,
    I’ve done all instructions, and pasted the code in my theme funcions and then did the test, but it didnt work.
    How to solve this problem?

      1. CF7 3.9 change access to important variables, like posted_data specifically. Lines 5 and 6 should be changed to:

        `$submission = WPCF7_Submission::get_instance();
        $formdata = $submission->get_posted_data();`

        There are also some differences in getting the value of checkboxes, so `$formdata[‘mailchimp-optin’]` should be `$formdata[‘mailchimp-optin’][0]`.

    1. Hey Fabricio

      Can you specify what isn’t working?

      What did you do?
      What did you see?
      What did you expect?

      For additional debugging…
      Pull up a Chrome Inspector or FireFox Firebug to see if there are any errors being generated.

      Also – make sure your capitalisation on MCAPI.class.php is correct.


  11. I have followed to tutorial instructiones but the subscriber didnt added to Mailchimp list.Only I dont know what I have done wong I have added the form code except the [ text* your-name 58/ ] 58 number.I have only write [text* your-name]..Please help me to solve this….

        1. Please, anyone can help me?I have tried everything but it dont work…..Really I dont know what the problem can be.

  12. BTW, you can use `[[shortcode]]` in the editor to output “[shortcode]” on the front end. I didn’t know either until a few days ago when I repurposed it for a customization.

  13. hi
    i’m wondering if it’s possible to do the same thing with the checkout page on woocommerce ?
    and possibly without an additional plugin…


Comments are closed.