The new MailChimp Integration Extension includes a user API in version 2.0. This can be used to call information about any specific list member without having to worry about formatting the request to send through the actual MailChimp API. The extension’s user API does this for you.
This is an example using the extension’s API function wpmem_mc_get_list_member() function to retrieve the list member’s MailChimp information and display some of it on the admin user profile. We will use the WP-Members action hook wpmem_admin_after_profile to display this at the bottom of the WP-Members section of the profile. The end result will add a a line like the image below to the WP-Members section of the user profile
The wpmem_mc_get_list_member() function requires the either the user’s ID or their email address. Since the wpmem_admin_after_profile action does not readily have the email address, we’ll pass the user ID in our demonstration.
The result from the function is the response from the MailChimp API, which will be an array of values with the user’s data. Most of this is not relevant to our example. If you want to know what’s in the full result, use print_r() or var_dump() to display the result on screen so you can look through it. For our purposes, we’ll be focused on displaying results in three elements of the array:
The stats has two values that we will display in this example:
Both of these values are passed as raw numbers, so we will format them as a percentage using php’s sprintf, taking the raw number, multiplying by 100, and then displaying as a percentage. That can be done like this:
The member_rating value is a number, 1-5. You generally see this displayed in your MailChimp list as a star rating for each user. We will recreate that here using a little php magic and a star from the WordPress dashicons that are already part of WordPress – no need to reinvent the wheel.
This creates a loop that runs 5 times ($i = 1 – 5). For each step through the loop, we compare the loop count to the member_rating value. Each time we add either a colored version of the star or a gray version of the star. At the end we have five stars, with the number of yellow stars equal to the member’s rating value.
Lastly, we use the stats value for a couple of things. We want to display the status which will be one of the following if they are on the list:
- pending (user has not acted on the double opt-in email that was sent yet)
But we also use the status value for another important thing in this script. If the user turns out to not be on the list, we need to display information accordingly rather than something generic. If the status value is “404” then the MailChimp API returned an unknown for the user, which basically means they have not subscribed. We set up an if statement to handle this.
If the user is 404, then we indicate they are not subscribed, otherwise we echo the values we have for status, open rate, click rate, and the star rating.
Putting that altogether, here is the script for adding this to the user profile. You can use this as is or modify it to display the MailChimp subscriber details that are relevant to you.