How to get the number of MailChimp subscribers in WordPress

MailChimp

If you run a WordPress blog or magazine, then you are probably doing an email newsletter to your readers. And if you’re using MailChimp, you’re in luck – in this tutorial, we’ll show you how to display the number of subscribers on your site using the MailChimp API.

This tutorial is intended for those who are already familiar with the HTTP API in WordPress. If you are hearing about this for the first time, we advise you to review the code first.

MailChimp API

In our introductory article about MailChimp, we touched a little on the topic of API – this is a programming interface that allows you to work with the service using code. This way you can manage subscribers, lists, campaigns, reports, and much more, but today we will only look at how to get and display the number of subscribers.

To work with the MailChimp API, you must first obtain an API key. You can do this in the Account → Extras → API Keys section in the MailChimp web interface or follow the direct link.

You will also need the listing ID, which can be found in its settings in the web interface, and the data center ID – these are the last three characters of your API key, for example, us6.

Full documentation for the latest version of the API can be found on the MailChimp developer site. Pay attention to the authentication section, and the method lists/{list_id}you will use to request information about your list.

Authentication

The new version of MailChimp API (3.0) has a full-fledged authentication. If you are developing a third-party application for general use, then you should look towards OAuth authentication, and if you only need to display the number of subscribers on your site, then Basic authentication using an API key is fine:

$request = wp_remote_get( $url, array(
'headers' => array(
'Authorization' => 'Basic ' . base64_encode( $username . ':' . $api_key ),
),
) );

Here $usernameand $api_keythis is your username in MailChimp, and the API key you created, respectively. You will need to pass a similar header every time you access the API.

Request for lists/{list_id}

You can get information about a list in MailChimp using the lists/{list_id} method, where {list_id} is the ID of your list.

if ( wp_remote_retrieve_response_code( $request ) == 200 ) {
$body = wp_remote_retrieve_body( $request );
$body = json_decode( $body, true );
$count = absint( $body['stats']['member_count'] );
}

Thus, the function wp_remote_get()will make a request to the MailChimp API for information about your list.

The result must be processed with the and functions wp_remote_retrieve_response_code(), wp_remote_retrieve_body()and since MailChimp returns data in JSON format by default, it must also be decoded with the function json_decode():

if ( wp_remote_retrieve_response_code( $request ) == 200 ) {
$body = wp_remote_retrieve_body( $request );
$body = json_decode( $body, true );
$count = absint( $body['stats']['member_count'] );
}

$countHere, the number of subscribers to the given list will be written to the variable. If you are interested in information about the list other than the number of subscribers, look at the array $body, for example using the function print_r().

Also see: Best Services for Monitoring WordPress Sites

You can wrap all this code in a function and call it where you need to display the number of subscribers, for example, in a WordPress widget. But keep in mind that it is too “expensive” to execute the request every time for the speed of your site, so the result must be cached.

Caching

The most suitable type of caching for such an operation is transits. With the help of a transit cache, you can save the number of subscribers and update it, for example, once every 12 hours, or more often / less often, depending on the dynamics of your list.

function mailchimp_get_subscribers_count() {
$cache_key = 'mailchimp-subscribers';
$count = get_transient( $cache_key );
if ( $count === false ) {
// Get quantity from MailChimp API
set_transient( $cache_key, $count, 12 * HOUR_IN_SECONDS );
}

return $count;
}

You can view the full function on the GitHub Gist. If you are working with a highly visited site, then we also recommend that you pay attention to the TLC-Transients project, which will allow you to update the cached data in the background.

Leave a comment

Your email address will not be published.