Select Page

For the past few days my spare moments were spent trying to connect Contact Form 7 to MailChimp in WordPress. I had a form on a website that I needed to pass the information to MailChimp effectively (no double-opt-in). Its seems that all the tutorials on the subject are old as this is probably a situational problem. No one tackles it until they need it.

Well, I needed it and here is what I found out. Contact Form 7 had a major update in version 3.9 and changed many of the former calls and hooks; which is why the older tutorials don’t work very well. Also, MailChimp has updated their API, there isn’t a large difference as far as what’s needed for this tutorial, but its worth noting. So, here is what I have figured out. Hopefully it will save you a little time.

As a point of reference. I like to create a plugin for these types of projects rather than just adding code the the theme’s functions file.

Here is what you need:

MailChimp PHP API – Download the Mailchimp.php file and the Mailchimp folder and add them to your project folder.

MailChimp API Key – In your MailChimp account, go to Account -> Extras -> Api Keys. You’ll have to generate one if you haven’t already.

MailChimp List Id – In your MailChimp account, go to the list you want to add contacts to, Settings -> List Name and Defaults. The number you need is in the right hand column, and it does not include the period at the end.

Ok, let’s set up the file.

First add your WordPress plugin info:

<?php
/**
* Plugin Name: Divi Theme Extender
* Plugin URI: http://joelwolfgang.com
* Description: This plugin is expressly designed for my personal clients as a personalized extention of the Elegant Themes Premium Theme Divi.
* Version: 0.1
* Author: Joel Wolfgang
* Author URI: http://joelwolfgang.com
* License: Premium
*/

Second let’s add our variables:

function wpcf7_send_to_mailchimp($cfdata) {

  $apikey = "----API KEY----";
  $listid = "----LIST ID----";
  $submission = WPCF7_Submission::get_instance();
  $form_data = $submission->get_posted_data();
  $send_this_email = $form_data['your-email'];
  $merge_vars = array(
    'FNAME' => $form_data['first-name'],
    'LNAME' => $form_data['last-name'],
    'PHONE' => $form_data['phone'],
  );

Third let’s add the remainder of the function:

  require('Mailchimp.php');
  $Mailchimp = new Mailchimp( $apikey );
  $Mailchimp_Lists = new Mailchimp_Lists( $Mailchimp );
  $subscriber = $Mailchimp_Lists->subscribe(
    $listid,
    array( 'email' => htmlentities($send_this_email)),
    $merge_vars,
    'html',
    false,
    true,
    true,
    false
 );

return;
}

add_action('wpcf7_before_send_mail', 'wpcf7_send_to_mailchimp', 1);

Here it is all together:

<?php
/**
* Plugin Name: Divi Theme Extender
* Plugin URI: http://joelwolfgang.com
* Description: This plugin is expressly designed for my personal clients as a personalized extension of the Elegant Themes Premium Theme Divi.
* Version: 0.1
* Author: Joel Wolfgang
* Author URI: http://joelwolfgang.com
* License: Premium
*/


function wpcf7_send_to_mailchimp($cfdata) {

  $apikey = "---API KEY----";
  $listid = "----LIST ID----";
  $submission = WPCF7_Submission::get_instance();
  $form_data = $submission->get_posted_data();
  $send_this_email = $form_data['your-email'];
  $merge_vars = array(
    'FNAME' => $form_data['first-name'],
    'LNAME' => $form_data['last-name'],
    'PHONE' => $form_data['phone'],
  );
  require('Mailchimp.php');
  $Mailchimp = new Mailchimp( $apikey );
  $Mailchimp_Lists = new Mailchimp_Lists( $Mailchimp );
  $subscriber = $Mailchimp_Lists->subscribe(
    $listid,
    array( 'email' => htmlentities($send_this_email)),
    $merge_vars,
    'html',
    false,
    true,
    true,
    false
 );

return;
}

add_action('wpcf7_before_send_mail', 'wpcf7_send_to_mailchimp', 1);

Its a pretty simple function. But it can help a lot while avoiding lost leads through Double opt-in. Enjoy!

Pin It on Pinterest

Shares

Share This

Share this post with your friends!