Skip to content

Instantly share code, notes, and snippets.

@zeropointdevelopment
Last active January 1, 2016 12:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zeropointdevelopment/8142647 to your computer and use it in GitHub Desktop.
Save zeropointdevelopment/8142647 to your computer and use it in GitHub Desktop.
[WordPress] Code from our blog post A MailChimp Opt-in Field for Contact Form 7 - http://www.limecanvas.com/a-mailchimp-opt-in-field-for-contact-form-7/
[ text* your-name 58/ ]
[ email* your-email 58/ ]
[ checkbox mailchimp-optin default:1 use_label_element "Subscribe to our newsletter" ]
/* MailChimp CF7 integration
* Uses <theme>/includes/MCAPI.class.php
*/
function wpcf7_send_to_mailchimp($cfdata) {
$formtitle = $cfdata->title;
$formdata = $cfdata->posted_data;
// Opt-in field checked?
if ( $formdata['mailchimp-optin'] ) {
$names = explode(' ',trim($formdata['your-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(
'FNAME'=>$firstName,
'LNAME'=>$lastName
);
// MCAPI.class.php needs to be in theme/includes folder
require_once('includes/MCAPI.class.php');
// grab an API Key from http://admin.mailchimp.com/account/api/
$api = new MCAPI('YOURAPIKEY');
// 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 = 'YOURLISTID';
// Send the form content to MailChimp List without double opt-in
$retval = $api->listSubscribe($list_id, $send_this_email, $mergeVars, 'html', false,true);
}
}
add_action('wpcf7_mail_sent', 'wpcf7_send_to_mailchimp', 1);
@anahitaida
Copy link

I replaced the API key and Listing ID.But seems it doesnt work.Subscribers didnt added to Mailchimp list.Please help me to solve this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment