Using mailchimp WP plugin without the form

If you've read some of my other posts like Web form security - avoiding common mistakes , you know that I like to think of forms as just a GUI for an API endpoint (do go read that article if that point isn't clear to you - I feel it is pretty importnat for understanding web development). While working today with the Mailchimp for WordPress plugin, I thought it was a great opportunity to demonstrate this and show you how to create Mailchimp subscribers without using a form.

Why would we do this, first of all? Let's imagine a case where we have many different newsletters, which people can subscribe to without being logged in, but we'd also like them to have access to a logged in page where they can subscribe/unsubscribe with simple button pushes. The Mailchip WP plugin is wonderful in that it allows us to setup our account and api key and share it everywhere in our application, but it sort of expects us to use a form (plugin generated or of our own) to handle the user interaction. Let's dive under the hood for a moment and see how we can just call the parts we want - and at the same time learn a lot about working with WP plugins in general.

Install and activate the Mailchimp for WP plugin (including adding your Mailchimp API key in the admin) and then go have a look at the files themselves in your plugins directory. Our main plugin file is mailchimp-for-wp.php, which has some messy looking code but really does nothing more than include the needed files and load the plugin itself. Nothing to see here - move on.

The includes directory is where we will go next. Three files in particular interest us - the class-plugin.php, class-form.php and class-api.php. Right away this tells us something - our form and api different parts. The code is a little complicated but if you look in the class-form.php you'll see:

public function submit() {	
   ...
   $success = $this->subscribe($data);
   ...
}

public function subscribe( array $data ) {
   ...
   $api = mc4wp_get_api();
   $opts = mc4wp_get_options('form');
   ...
}

Which means that all that is actually happening is the form is collecting the needed data and passing it to the API to send to Mailchimp. So, let's cut out the middleman and do that task ourselves.

We have two options, but let's stick with the easier one and use our main plugin class, MC4WP_Lite. This is already loaded (since it is a plugin) and so accessible to us anywhere in our code. It has another advantage in that it is set to load our API key and any other options we may want via the get_api() function, and so takes some of the burden off of us for reading and verifying these. So, whereever we are working with the code behind our "Subscribe" button, we'd want something like this:

$MC4WP_Lite = new MC4WP_Lite();

//creates a new MC4WP_Lite_API object
$api = $MC4WP_Lite->get_api();	

//check it works
$api->is_connected();

if ($api->has_error())
{
	echo $api->get_error_message();
}

DO! use the error handling bit, for here and later. Mailchimp has very informative messages and you will save yourself some puzzling.

Now we have a connection object to the Mailchimp API, so let's send something across. For this excercse, we'll just add a new subscriber, as if they had clicked a button from their profile page. Looking at the class-api.php, we see there is a function subscribe() with a whole list of parameters. These params are fairly well explained by their names, and since most of them are optional, we'll just make a simple version.

//The mailing list id is found on the list page in Mailchimp, at the very bottom
//It will be a random alphanumeric string similar to 'abcd456a12'	

//In this case, I've set up a AFC option field, but you can do as best suits you  
$list_id = get_field('mail_chimp_list_id', 'option');

//Use your favorite trick to get the user info (or try mine A cleaner WP User object )
$current_user = wp_get_current_user();

//The API is already prepared to handle a few of these, such as  
$merge_vars = array(
	'FNAME'      => $current_user->user_firstname, 
	'LNAME'      => $current_user->user_lastname, 
);

$email = $current_user->user_email

$api->subscribe( $list_id, $email, $merge_vars);	

if ($api->has_error())
{
	return $api->get_error_message();
}	

return 'Subscribed';

And that's it! Run that, and you should get a subscription email (becuase this is double opt-in be default) that you can confirm. Exploring further the API library (or upgrading to the pro version), you'll see functions for unsubscribing and other options you may need.

Hopefully this has not only provided some practical code, but also given you a little better understanding of what goes on under the hood of some of these plugins, and how you can take advantage of that knowledge to use them in different ways. Many other plugins such as WooCommerce suite can used in this same way - it just takes a little code reading.

Contact me