Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Sep 21st, 2012  |  syntax: None  |  size: 3.01 KB  |  hits: 14  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. <?php
  2. /*
  3.         Use: Add this code to the Wordpress theme's functions.php file.
  4.         Requires Mailchimp's MCAPI.class.php in the same folder as the theme's functions.php file.
  5.  
  6.         Hi. I've tweaked this fork to add FNAME LNAME and GROUPINGS support, making this a super easy way to
  7.         use CF7 with Mailchimp's API, with a checkbox asking if they want to subscribe, without double-opt-in verification.
  8.  
  9.         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:
  10.  
  11.         <p>First Name (required)<br />[text* first-name] </p>
  12.         <p>Last Name (required)<br />[text* last-name] </p>
  13.         <p>Your Email (required)<br />[email* your-email] </p>
  14.         <p>Subject<br />[text your-subject] </p>
  15.         <p>Your Message<br />[textarea your-message] </p>
  16.         <p>Subscribe to our awesome newsletter?<br />[checkbox subscribe default:1 "Of course"]</p>
  17.         <p>[submit "Send"]</p>
  18.  
  19.         Groupings is used this way: Create a new Grouping in Mailchimp, named "Form Used", and create groups
  20.         named the exact name of your CF7 forms. If no group exists for that form name, the subscribe will FAIL, FYI.
  21.  
  22.         If you don't want to use groupings, change:
  23.  
  24.            'LNAME'=> $formdata['last-name'],
  25.            'GROUPINGS'=>array( array('name'=>'Form Used', 'groups'=>$formtitle),
  26.            ));
  27.  
  28.            to
  29.  
  30.            'LNAME'=> $formdata['last-name']);
  31.  
  32.         Simple enough. Change "Form Used" in the code if you want a different Groupings name, such as "Interested In",
  33.         with each form named "Inquiry", "Quote", "Contest", etc, tracking the source in Mailchimp of each subscriber.
  34.  
  35.         If you prefer to have the subscriber verify their email first before adding, change:
  36.  
  37.         $retval = $api->listSubscribe($list_id, $send_this_email, $mergeVars, 'html', false);
  38.         to:
  39.         $retval = $api->listSubscribe($list_id, $send_this_email, $mergeVars, 'html', true);
  40.  
  41.         I may tackle adding variables for turning groupings and double opt-in on/off later, & turn this into a plugin.
  42.  
  43.         Enjoy! Hit me up at http://webwizards.net/ if you have any questions or comments.
  44.  
  45.         Rob
  46.  
  47.         Inspired by http://www.bigbossmas.com/wordpress/integrating-contact-form-7-to-mailchimp-the-better-way/
  48.  
  49. */
  50.  
  51. function wpcf7_send_to_mailchimp($cfdata) {
  52.  
  53. $formtitle = $cfdata->title;
  54. $formdata = $cfdata->posted_data;
  55.  
  56.  if ( $formdata['subscribe'] ) {
  57.  
  58.    $send_this_email = $formdata['your-email'];
  59.    $mergeVars = array(
  60.    'FNAME'=>$formdata['first-name'],
  61.    'LNAME'=> $formdata['last-name'],
  62.    'GROUPINGS'=>array( array('name'=>'Form Used', 'groups'=>$formtitle),
  63.    ));
  64.  
  65.    // MCAPI.class.php needs to be in theme's root folder with functions.php
  66.    require_once('MCAPI.class.php');
  67.  
  68.    // grab an API Key from http://admin.mailchimp.com/account/api/
  69.    $api = new MCAPI('--- YOUR API KEY HERE ---');
  70.  
  71.    // grab your List's Unique Id by going to http://admin.mailchimp.com/lists/
  72.    $list_id = "--- YOUR LIST ID HERE ---";
  73.  
  74.    // Send the form content to MailChimp List without double opt-in
  75.    $retval = $api->listSubscribe($list_id, $send_this_email, $mergeVars, 'html', false);
  76.  
  77.   }
  78. }
  79. add_action('wpcf7_mail_sent', 'wpcf7_send_to_mailchimp', 1);
  80. ?>