Skip to content

Instantly share code, notes, and snippets.

@vmdao
Forked from downtownrob/functions.php
Created August 22, 2019 09:59
Show Gist options
  • Save vmdao/3c20f5b53f2d5d74526a68b794985180 to your computer and use it in GitHub Desktop.
Save vmdao/3c20f5b53f2d5d74526a68b794985180 to your computer and use it in GitHub Desktop.
Saving Contact Form 7 data into a MailChimp List with checkbox and groupings
<?php
/*
Use: Add this code to the Wordpress theme's functions.php file.
Requires Mailchimp's MCAPI.class.php in the same folder as the theme's functions.php file.
Hi. I've tweaked this fork to add FNAME LNAME and GROUPINGS support, making this a super easy way to
use CF7 with Mailchimp's API, with a checkbox asking if they want to subscribe, without double-opt-in verification.
Use: Create a CF7 form with text fields first-name, last-name, your-email, and a checkbox field named subscribe, plus any others you like:
<p>First Name (required)<br />[text* first-name] </p>
<p>Last Name (required)<br />[text* last-name] </p>
<p>Your Email (required)<br />[email* your-email] </p>
<p>Subject<br />[text your-subject] </p>
<p>Your Message<br />[textarea your-message] </p>
<p>Subscribe to our awesome newsletter?<br />[checkbox subscribe default:1 "Of course"]</p>
<p>[submit "Send"]</p>
Groupings is used this way: Create a new Grouping in Mailchimp, named "Form Used", and create groups
named the exact name of your CF7 forms. If no group exists for that form name, the subscribe will FAIL, FYI.
If you don't want to use groupings, change:
'LNAME'=> $formdata['last-name'],
'GROUPINGS'=>array( array('name'=>'Form Used', 'groups'=>$formtitle),
));
to
'LNAME'=> $formdata['last-name']);
Simple enough. Change "Form Used" in the code if you want a different Groupings name, such as "Interested In",
with each form named "Inquiry", "Quote", "Contest", etc, tracking the source in Mailchimp of each subscriber.
If you prefer to have the subscriber verify their email first before adding, change:
$retval = $api->listSubscribe($list_id, $send_this_email, $mergeVars, 'html', false);
to:
$retval = $api->listSubscribe($list_id, $send_this_email, $mergeVars, 'html', true);
I may tackle adding variables for turning groupings and double opt-in on/off later, & turn this into a plugin.
Enjoy! Hit me up at http://webwizards.net/ if you have any questions or comments.
Rob
Inspired by http://www.bigbossmas.com/wordpress/integrating-contact-form-7-to-mailchimp-the-better-way/
*/
function wpcf7_send_to_mailchimp($cfdata) {
$formtitle = $cfdata->title;
$formdata = $cfdata->posted_data;
if ( $formdata['subscribe'] ) {
$send_this_email = $formdata['your-email'];
$mergeVars = array(
'FNAME'=>$formdata['first-name'],
'LNAME'=> $formdata['last-name'],
'GROUPINGS'=>array( array('name'=>'Form Used', 'groups'=>$formtitle),
));
// MCAPI.class.php needs to be in theme's root folder with functions.php
require_once(TEMPLATEPATH . '/MCAPI.class.php');
// grab an API Key from http://admin.mailchimp.com/account/api/
$api = new MCAPI('--- YOUR API KEY HERE ---');
// grab your List's Unique Id by going to http://admin.mailchimp.com/lists/
$list_id = "--- YOUR LIST ID HERE ---";
// 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);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment