Using the MailChimp API to check a WordPress user’s subscription status

Is this user on my MailChimp list?

Ben Zumdahl
Apr 6, 2018 · 3 min read

Many of our clients with WordPress websites use MailChimp for email marketing. Its barrier to entry is nonexistent (free!), the interface is top notch, and website integrations abound: MailChimp has an official plugin, form providers like Gravity Forms offer add-ons, and third party plugins are numerous.

But here’s a scenario these integrations don’t address: I want to know if the user who logs into my website is on my MailChimp list. Based on this information (is this user a subscriber: yes or no) we can provide better prompts to the user and a better on-site experience. At a minimum, we can stop prompting the user to sign up — because we know she’s already signed up. Instead, we can prompt her toward another type of engagement.

So, how to accomplish this? We turn to MailChimp’s API.

Version 3.0 of MailChimp’s API is well documented but it doesn’t provide any quick start examples for specific coding languages. This keeps it out of reach for developers not yet comfortable delving into and unpacking new APIs. Below is one way to use the API to tackle the basic task of checking a specific user’s email address to see if it is on a particular MailChimp list.

Here’s the code — scroll down for a few tips on using it:

function get_subscriber_mailchimp_status($user_login, $user) {$api_key = 'mailchimp_api_key';
$list_id = 'mailchimp_list_id';
$us = 'mailchimp_us'; // eg, 'us5' or 'us7'
$args = array(
'headers' => array(
'Authorization' => 'Basic ' . base64_encode( 'user:' . $api_key ),
'Access-Control-Allow-Origin' => '*',
$email_address = $user->user_email;
$email_formatted = md5(strtolower($email_address));
$response = wp_remote_get( 'https://'. $us .''. $list_id .'/members/' . $email_formatted, $args );$body = json_decode( wp_remote_retrieve_body( $response ) );$mailchimp_status = $body->status;if($mailchimp_status == 'subscribed'):
update_user_meta( $user->ID, 'user_mc_subscriber_status', 'yes' );
update_user_meta( $user->ID, 'user_mc_subscriber_status', 'no' );
}add_action('wp_login', 'get_subscriber_mailchimp_status', 10, 2);

There are three variables you need to set to make this work:

  1. $api_key: Create and add a MailChimp API key

This function is set to run when a user logs into your website (see the last line of code above). You could hinge it on another action, but as is, each time a user logs in, his MailChimp subscriber status is checked. If the result confirms that he is a subscriber, then a custom meta field named ‘user_mc_subscriber_status’ is updated with the value of ‘yes’. If not, it is updated with a value of ‘no’.

Now what?

Now that you’re storing this yes/no value on the user record you can check that value and do something based on it. Here’s how to check it:

if(get_user_meta(get_current_user_id(), 'user_mc_subscriber_status', 'true') == 'yes'):// do something for subscriberselse:// do something for non-subscribersendif;

You can use this if-else check throughout the website to provide differentiated content for MailChimp subscribers and non-subscribers!

Want to take the next step and do even more with MailChimp on your WordPress website? Let us help you build a solution that’s just right for your needs:

Fiat Insight

Building products with purpose. Since 2010.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store