nucliweb
3/12/2012 - 10:47 AM

Saving Contact Form 7 data into a MailChimp List

Saving Contact Form 7 data into a MailChimp List

<?php
/*
	Guardando email de Contact Form 7 en MailChimp

	Necesita el archivo MCAPI.class.php de la API de MailChimp

	En este en concreto usamos un checbox con el nombre subscribe para comprobar si el 
	usuario quiere subscribirse o no. En caso afirmativo guardamos el nombre y el email 
	en la lista concreta de nuestro MailChimp.

	// 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['your-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 LIST -----";

		// 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);
?>