How to Sync WordPress User Registrations to Mailchimp

In this tutorial I will show you two different ways how you can sync WordPress user registrations to Mailchimp. The first way is more for developers, we are going to create API requests, the second one – for regular users, so all you have to do is just to install the plugin.

Sync User Registrations with Mailchimp API

In this first method we are going to configure WordPress user registrations syncronization with some code. If a code solution is not what you’re looking for then I recommend you to skip to the second part of this tutorial.

user_register

Everything begins with this hook. Hook user_register in WordPress will be fired in the following situations:

  • When a user registers on the website.
  • When administrators register a user via admin area Users > Add New.
  • When user is registered via a function in the code, for example with wp_insert_user() or wp_create_user().

So everything begins with running user_register hook somewhere on your website.

add_action( 'user_register', 'rudr_sync_wp_users_to_mailchimp', 10, 2 );

function rudr_sync_wp_users_to_mailchimp( $user_id, $userdata ) {
	
	// do some Mailchimp API stuff
	
}

Please not that $userdata parameter was added in WordPress 5.8, so if you’re using an earlier version of WordPress, you have to get user emails from ID with get_user_by() function.

Interacting with Mailchimp API using WordPress HTTP API

Now, since we know where we run the requests to Mailchimp API, it is time to create them. There are a lot different ways how you can perform them, the most popular one over the internet is with cURL, but since we’re working with WordPress, I assume it would be a more correct and clean way to use WordPress functions for that, wouldn’t it?

I have a different tutorial, where I describe how to perform Mailchimp API requests using WordPress HTTP API functions, so if you want to learn a bit more about it, please check it.

And here is the complete code for our goal:

add_action( 'user_register', 'rudr_sync_wp_users_to_mailchimp', 10, 2 );

function rudr_sync_wp_users_to_mailchimp( $user_id, $userdata ) {
	
	$list_id = 'YOUR AUDIENCE ID';
	$api = 'YOUR API KEY';
	$email = $userdata->user_email;
	
	$response = wp_remote_request( 
		'https://' . substr($api,strpos($api,'-')+1) . '.api.mailchimp.com/3.0/lists/' . $list_id . '/members/' . md5(strtolower($email)),
		array(
			'method' => 'PUT',
	 		'headers' => array(
				'Authorization' => 'Basic ' . base64_encode( 'user:'. $api )
			),
			'body' => json_encode(
				array(
					'email_address' => $email,
					'status' => 'subscribed' 
				)
			)
		)
	);

}

Believe or not, but this is our final code for your functions.php of wherever you’re going to use it. Let me highlight a couple moments:

  • You have to decide to what Mailchimp audience (list) you’re going to subscribe newly registered WordPress users, here is how to get an audience ID. In case you would like to subscribe your users to different lists depending on their user role on the website, then take a look at the plugin solution.
  • You will also need a Mailchimp API key, here is how to get it.
  • The last but not the least, if you respect the users privacy, probably you could change the status on line 19 from subscribed to pending, in this case users will get a confirmation email before they are going to be subscribed to a list.

Sync User Registrations with WordPress plugin

I hope the API solution seems clear and simple for you. In case it doesn’t or maybe you just need a little bit more customization, I recommend you an “install-and-use” solution, which means that all you have to do is to install my Simple Mailchimp Sync plugin for WordPress, all the work it will do for you.

Just check how cool its settings are:

Sync WordPress User Registrations to Mailchimp
The settings page of Simple Mailchimp Sync plugin for WordPress.

So you can:

  • Sync every WordPress user role to a different Mailchimp audience (yes, I have already mentioned it before).
  • Add tags to every rule. In case your Mailchimp pricing plan doesn’t allow you to create more audiences, you can use the single audience and segment users by tags!
  • Provide more user data to Mailchimp like first name, last name and whatever else.
  • And of course it allows you to bulk re-sync all existing users on your WordPress website.
Misha Rudrastyh

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

Follow me on X