Integrating Contact Form 7 to MailChimp – the Better Way
Recently, I had to figure out how to integrate excellent Contact Form 7 plugin for WordPress to a MailChimp list, so when someone uses the contact form on the site, his/her name and email address are added to MailChimp list. I thought it won’t be that hard to find the answer, since MailChimp offers an array of API calls, but to the contrary, my search didn’t really get me what I was looking for, so now that I figured it out, I thought I’d put it out there for others to use.
My initial search yielded this post, suggesting that I modify the Contact Form 7 code itself, which is what I definitely didn’t want to do. I also found this post which talks about using wpcf7_mail_sent action hook to send form data to a custom database. So with some reference to the MailChimp listSubscribe API, I came up with a solution to use contact form action hook to subscribe contact to a MailChimp list.
First, you need:
- MailChimp Account, and a list to add the contact info to.
- MailChimp API key ( Account -> API Keys & Authorized Apps )
- Unique ID for the list ( List Name -> Settings -> Settings and Unique ID )
- MCAPI.class.php file, which is available here. ( UPDATED 2012.10.12: Scroll down to section “PHP” and download the MCAPI mini v1.3.1 file. Download the MCAPI v1.3.1 (current) if you want to reference the help text in there. )
- A contact form using Contact Form 7. I modified it to collect First Name and Last Name separately so it matches the MailChimp list.
- Groups in the MailChimp list that matches the name of the contact forms (if you want to separate your contact into different subgroups).
Then, put the MCAPI.class.php file in your theme folder, and add the following code to your function.php file, replacing API KEY and Unique ID with your own:
function wpcf7_send_to_mailchimp($cfdata) { $formtitle = $cfdata->title; $formdata = $cfdata->posted_data; $send_this_email = $formdata['your-email']; $mergeVars = array( 'FNAME'=>$formdata['your-first-name'], 'LNAME'=>$formdata['your-last-name'], 'GROUPINGS'=>array( array('name'=>'Form Used', 'groups'=>$formtitle), )); // MCAPI.class.php needs to be in theme folder require_once('MCAPI.class.php'); // grab an API Key from http://admin.mailchimp.com/account/api/ $api = new MCAPI('--------API KEY---------'); // 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. $list_id = "---------Unique ID---------"; // Send the form content to MailChimp List without double opt-in $retval = $api->listSubscribe($list_id, $send_this_email, $mergeVars, 'html', false); } add_action('wpcf7_mail_sent', 'wpcf7_send_to_mailchimp', 1);
That’s it! Now when someone uses a contact form, the contact info is added to the MailChimp list. Piece of cake, eh?
What the code does:
The add_action at the end hooks my function “wpcf7_send_to_mailchimp” to the action hook “wpcf7_mail_sent” which gets executed after the contact form mail is sent. The action hook sends the data from the contact form, from which I pull the title of the contact form, first name, last name, and email field. Then I create the merge data which I send off to the API.
This creates the merge data:
$mergeVars = array( 'FNAME'=>$formdata['your-first-name'], 'LNAME'=> $formdata['your-last-name'], 'GROUPINGS'=>array( array('name'=>'Form Used', 'groups'=>$formtitle), ));
In the contact form, I have set up fields for first name and last name, which I named “your-first-name” and “your-last-name.” These get assigned to “FNAME” and “LNAME” which are the merge tags in MailChimp list. I have also set up a group in MailChimp list called “Form Used,” and named all the subgroups using same names as the contact form list. This way, I can assign each contact into a different subgroup in the MailChimip list depending on the contact form used. Notice that it allows you to put a contact into multiple groups in “GROUPINGS” array, so that’s why it’s in an array within an array. Alternatively, you can create conditional statements using the form title so that you can select which contact form triggers the list subscription.
This is the crux of the code which sends the email and merge variable off to the API:
$retval = $api->listSubscribe($list_id, $send_this_email, $mergeVars, 'html', false);
$retval is the returned value from the API call which is true (success) or false (error), but for my code I’m really not doing anything with it. Key here is that I am setting the double opt-in to false (the last argument), so a contact is added automatically to the list without confirmation, making the whole process transparent. Looking at the MCAPI.list.php, listSubscribe takes following arguments:
listSubscribe( $id, $email_address, $merge_vars=NULL, $email_type='html', $double_optin=true, $update_existing=false, $replace_interests=true, $send_welcome=false )
Here is the full explanation of the arguments:
- $id (string) the list id to connect to.
- $email_address (string) the email address to subscribe.
- $merge_vars (array – optional) merges for the email (FNAME, LNAME, etc.) Note that a merge field can only hold up to 255 bytes.
- $email_type (string – optional) email type preference for the email either: ‘html,’ ‘text,’ or ‘mobile.’ (defaults to ‘html’)
- $double_optin (true/false – optional) flag to control whether a double opt-in confirmation message is sent. (defaults to true) Abusing this may cause your account to be suspended.
- $update_existing (true/false – optional) flag to control whether a existing subscribers should be updated instead of throwing and error. (defaults to false)
- $replace_interests (true/false – optional) flag to determine whether we replace the interest groups with the groups provided, or we add the provided groups to the member’s interest groups. (defaults to true)
- $send_welcome (true/false – optional) if your double_optin is false and this is true, we will send your lists Welcome Email if this subscribe succeeds – this will *not* fire if we end up updating an existing subscriber. If double_optin is true, this has no effect. (defaults to false)
You can add more to the merge variable than I did, if you have more fields in the list, but there are some strict formatting that you need to follow for certain field types. See the comment in the MCAPI.class.php file (line 1541). There is a 250 character limit to merge variable, so I think if you are looking to capture lot of form data, you may be better off just creating the whole form within MailChimp. The drawback to that is of course you will be stuck with double opt-in which doesn’t make sense for a contact form – unless you create a form using an API…
Happy coding!
UPDATE – 2012.10.12:
Finally an update I’ve been meaning to write to give few more code examples and a debug trick that I use. Hope this will help.
1: Barebones code to check if your API is working
Test your Form with a real email address that’s not in your MailChimp list already. This only sends contact email address to MailChimip List with double opt-in. You should get two emails: one from Contact Form 7 to your contact address, and one from MailChimp to your test email address for opt-in confirmation.
function wpcf7_send_to_mailchimp($cfdata) { $formdata = $cfdata->posted_data; $send_this_email = $formdata['your-email']; // MCAPI.class.php needs to be in theme folder require_once('MCAPI.class.php'); // grab an API Key from http://admin.mailchimp.com/account/api/ $api = new MCAPI('--------API KEY---------'); // 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. $list_id = "---------Unique ID---------"; // Send the form content to MailChimp List without double opt-in $retval = $api->listSubscribe($list_id, $send_this_email); } add_action('wpcf7_mail_sent', 'wpcf7_send_to_mailchimp', 1);
2: Barebones code with a debug function
I recommend using this to get your mergeVars in order. The function is same as the barebones example above, but this will print some data in “devlog.txt” file on your server root folder, so you can check your form data from Contact Form and the mergeVar that you are trying to create (you may need to create this file beforehand).
function wpcf7_send_to_mailchimp($cfdata) { $formtitle = $cfdata->title; $formdata = $cfdata->posted_data; $send_this_email = $formdata['your-email']; $mergeVars = array( 'FNAME'=>$formdata['your-first-name'], 'LNAME'=> $formdata['your-last-name'], 'GROUPINGS'=>array( array('name'=>'Form Used', 'groups'=>$formtitle), )); // MCAPI.class.php needs to be in theme folder require_once('MCAPI.class.php'); // grab an API Key from http://admin.mailchimp.com/account/api/ $api = new MCAPI('--------API KEY---------'); // 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. $list_id = "---------Unique ID---------"; // Send the form content to MailChimp List without double opt-in $retval = $api->listSubscribe($list_id, $send_this_email); //this is for debug purposes $debug = array( 'time' => date('Y.m.d H:i:s e P'), 'formtitle' => $formtitle, 'formdata' => $formdata, 'mergevars' => $mergeVars, 'result' => $retval ); $test_file = fopen('devlog.txt', 'a'); $test_result = fwrite($test_file, print_r($debug, TRUE)); } add_action('wpcf7_mail_sent', 'wpcf7_send_to_mailchimp', 1);
In the devlog.txt file, you should see
- date and time when it was processed
- form title
- form data (array)
- mergeVars (array)
- returned value from MailChimp API (‘1’ for success, ‘0’ for fail)
Here is an example of devlog.txt. I set up a test form titled “MailChimp Test” with first name, last name, email, subject, message, as well as a checkbox for opt-in (named “mailchimp-opt-in”). For this example the checkbox was checked.
Array ( [time] => 2012.10.12 21:22:25 UTC +00:00 [formtitle] => MailChimp Test [formdata] => Array ( [_wpcf7] => 214 [_wpcf7_version] => 3.3 [_wpcf7_unit_tag] => wpcf7-f214-p215-o1 [_wpnonce] => d37d3e08ac [your-first-name] => Test [your-last-name] => User [your-email] => -----my test email--- [your-subject] => Test [your-message] => This is a test message. [mailchimp-opt-in] => Array ( [0] => Please check if you would like to sign up for the newsletter. ) [_wpcf7_is_ajax_call] => 1 ) [mergevars] => Array ( [FNAME] => Test [LNAME] => User [GROUPINGS] => Array ( [0] => Array ( [name] => Form Used [groups] => MailChimp Test ) ) ) [result] => 1 )
3: Simple Integration with just First Name and Last Name
Here is the simple example with just the first name and last name. This should automatically add contact to your list.
function wpcf7_send_to_mailchimp($cfdata) { $formtitle = $cfdata->title; $formdata = $cfdata->posted_data; $send_this_email = $formdata['your-email']; $mergeVars = array( 'FNAME'=>$formdata['your-first-name'], 'LNAME'=> $formdata['your-last-name'] ); // MCAPI.class.php needs to be in theme folder require_once('MCAPI.class.php'); // grab an API Key from http://admin.mailchimp.com/account/api/ $api = new MCAPI('--------API KEY---------'); // 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. $list_id = "---------Unique ID---------"; // Send the form content to MailChimp List without double opt-in $retval = $api->listSubscribe($list_id, $send_this_email, $mergeVars, 'html', false); } add_action('wpcf7_mail_sent', 'wpcf7_send_to_mailchimp', 1);
4: Simple Integration with Opt-In checkbox
For this to work, you need a checkbox in your contact form with single option. I set up a contact form with a checkbox named “mailchimp-opt-in”. Since this value will be empty if unchecked, I just do the simple if statement.
function wpcf7_send_to_mailchimp($cfdata) { $formtitle = $cfdata->title; $formdata = $cfdata->posted_data; if( $formdata['mailchimp-opt-in'] ) { $send_this_email = $formdata['your-email']; $mergeVars = array( 'FNAME'=>$formdata['your-first-name'], 'LNAME'=>$formdata['your-last-name'] ); // MCAPI.class.php needs to be in theme folder require_once('MCAPI.class.php'); // grab an API Key from http://admin.mailchimp.com/account/api/ $api = new MCAPI('--------API KEY---------'); // 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. $list_id = "---------Unique ID---------"; // Send the form content to MailChimp List without double opt-in $retval = $api->listSubscribe($list_id, $send_this_email, $mergeVars, 'html', false); } // end if } add_action('wpcf7_mail_sent', 'wpcf7_send_to_mailchimp', 1);