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

Is this user on my MailChimp list?

Ben Zumdahl
Fiat Insight
3 min readApr 6, 2018

--

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 .'api.mailchimp.com/3.0/lists/'. $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' );
else:
update_user_meta( $user->ID, 'user_mc_subscriber_status', 'no' );
endif;
}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
  2. $list_id: Get the ID of the MailChimp list you want to use
  3. $us: When you log into MailChimp, the URL looks something like this: https://us9.admin.mailchimp.com. The number in ‘us9’ varies by account; just grab the three digits and use them as this variable.

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: https://fiatinsight.com/contact

--

--