Last active
January 1, 2016 12:09
-
-
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/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ text* your-name 58/ ] | |
[ email* your-email 58/ ] | |
[ checkbox mailchimp-optin default:1 use_label_element "Subscribe to our newsletter" ] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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!