Sync Mailchimp Audience Members Updates to WordPress
In this tutorial I am going to show how you can sync any member preferences updates in Mailchimp audiences to WordPress users.
Sometimes I’m getting asked to add this functionality to my Simple Mailchimp Sync plugin, but every time I’m starting writing the code, I always come to a conclusion that the plugin will become “not simple” at all. So now it is possible only with a free add-on (or you can use code examples below for sure). Everything we need is just a little bit of configuration in Mailchimp account and a small code snippet.
Create a Mailchimp Webhook
Before creating any Mailchimp webhooks you have to know that every webhook relates to a specific Mailchimp audience. So if you’re going to sync multiple Mailchimp audiences to WordPress, you will need multiple webhooks.
Manually
Open an audience you need to configure a webhook for and then go Settings > Webhooks.

Then click the “Add new webhook” button and on a newly opened page select the following:

On the screenshot above:
- We’ve checked only “Profile updates” because in terms of this tutorial we are about to update user’s last name or something when it has been updated in the audience directly.
- Also let’s skip changes made via API, so it will not interfere with Simple Mailchimp Sync plugin in case you’re using it on your website.
- And the last but not the least, please pay attention to
?secret_key=secret_value
part in the webhook callback URL, you have to replace both parameters with some random values and don’t store them publicly. For example?mishawhook=jhcj89fjocksd123
.
Using Mailchimp API
In case you decided to create some kind of an add-on which does that automatically (or you can take a look at my add-on), you can use the code below to create webhooks. And yes, this code is using WordPress HTTP API.
$api_key = '';
$audience_id = '';
wp_remote_post(
'https://' . substr($api_key,strpos($api_key,'-')+1) . '.api.mailchimp.com/3.0/lists/' . $audience_id . '/webhooks/',
array(
'headers' => array(
'Authorization' => 'Basic ' . base64_encode( 'user:'. $api_key )
),
'body' => json_encode(
array(
'url' => 'https://test.rudrastyh.com/?secret_key=secret_value',
'events' => array(
'profile' => true,
),
'sources' => array(
'user' => true,
'admin' => true,
)
)
)
)
);
This piece of code should run only once and it will create exactly the same webhook we created manually before.
Process the Webhook on the Website
Here is just a simple code snippet that can go to your theme functions.php
file or something.
add_action( 'template_redirect', function(){
// exit the function in case it is not our hook
if( empty( $_GET[ 'secret_key' ] ) || 'secret_value' !== $_GET[ 'secret_key' ] ) {
return;
}
// check the action (it is totally optional in this example)
if( empty( $_POST[ 'type' ] ) || 'profile' !== $_POST[ 'type' ] ) {
return;
}
// we can also check the list ID to improve the security of the webhook
if( 'abcdef12345' !== $_POST[ 'data' ][ 'list_id' ] ) {
return;
}
// do the thing
$user = get_user_by( 'email', $_POST[ 'data' ][ 'email' ] );
if( $user ) {
update_user_meta( $user->ID, 'last_name', sanitize_text_field( $_POST[ 'data' ][ 'merges' ][ 'LNAME' ] ) );
}
exit;
} );

Misha Rudrastyh
Hey guys and welcome to my website. For more than 10 years I've been doing my best to share with you some superb WordPress guides and tips for free.
Need some developer help? Contact me
Hello Misha,
Thank you for your post! It’s a recent one, so I guess it’s still relevant, right?
I’m adding the exact same code on my website, but unfortunately in the “template_redirect” hook both $_GET and $_POST are always empty.
I got MC4WP plugin installed on my website, and I use its form to enable users subscribe to my newsletter. I see that users can normally fill in their email on the form and subscribe, and I see them as members on my account on Mailchimp.
However, as already mentioned, I can’t access their data using the “template_redirect” hook, $_POST[ ‘data’ ] etc. (“data” doesn’t even exist as an array key of $_POST). But I’d like to access their data, because I want to create a Coupon in WooCommerce, specifically for each new subscriber.
This piece of code (the one you have inside the “template_redirect” action) is supposed to run once a user subscribes through my website, once they confirm their subscription, right?
Hi Giorgos,
Did you try to test a webhook from Mailchimp dashboard?