Recently, I had an old client of mine get in touch with me to ask if it’s possible to display the number of email addresses he had subscribed to his Mailchimp account. He mentioned seeing something like this on other blogs (I think the JetPack email subscription component might have this feature) and was hoping he could do the same as a sort of “voucher” for his email list. My thought was that this is an interesting idea and could encourage some users to subscribe if they see that there are others benefiting.
Mailchimp provides a useful API but it’s not one that I had worked with so I did some digging. It turns out that retrieving a list’s size/count is not a difficult thing to do. In my opinion, it would have been even easier if Mailchimp updated the documentation for their API (I figured out how to do this by reading and playing with the code itself).
In this example, I’ll be showing you how to do this in WordPress. To retrieve a Mailchimp email-list’s count, follow these steps:
- Add the Mailchimp API files to a plugin or your current theme (I suggest going with a plugin). Since WordPress is written in PHP, you’ll need to include the PHP files found here.
- Add the code below to your plugin file or functions.php if you’re using a theme. Make sure to update the include path accordingly. Also, replace api_key_goes_here with your Mailchimp account API (under Account Settings) and list_id_goes_here with the ID of the list for which you want to retrieve the count (found under the list’s settings).
- Finally, the add_action() is specific to the Genesis Framework and will not work with other themes. If you are using something other than Genesis, you will need to change the hook or add the output to the appropriate theme file.