Sync WordPress User Profile Updates to Mailchimp and Vice Versa

In this tutorial we are going to work with both Mailchimp API and its webhooks. You can also find basic guides on how to work with the API and also a basic guide about webhooks on my website.

In the first part of the tutorial we are going to update user first and last name in WordPress profile and sync the changes with Mailchimp audience fields. Then we are going to update the merge tags directly in Mailchimp and configure the syncing with WP profiles.

Also I can recommend you my Simple Mailchimp Sync plugin that allows to sync users between WordPress and Mailchimp automatically.

Sync User Profile Update with Mailchimp Merge Tags

This part of the tutorial is the simplest, because all we need to do is to create a function connected to profile_update action hook and a Mailchimp API call.

add_action( 'profile_update', 'rudr_upd_user', 25, 3 );

function rudr_upd_user( $user_id, $old_userdata, $userdata ) {
	
	$list_id = 'YOUR AUDIENCE ID';
	$api = 'YOUR API KEY';
	
	// user data
	$email = $old_userdata->user_email;
	$first_name = $userdata->first_name;
	$last_name = $userdata->last_name;
	
	wp_remote_request( 
		'https://' . substr($api,strpos($api,'-')+1) . '.api.mailchimp.com/3.0/lists/' . $list_id . '/members/' . md5(strtolower($email)),
		array(
			'method' => 'PATCH',
		 	'headers' => array(
				'Authorization' => 'Basic ' . base64_encode( 'user:'. $api )
			),
			'body' => json_encode(
				array(
					'merge_fields' => array( 'FNAME' => $first_name, 'LNAME' => $last_name )
				)
			)
		)
	);

}

Here is also where to get API key and audience ID.

Sync Audience Fields Updates with WordPress User Profile

Create a webhook in Mailchimp

First of all go to your Mailchimp dashboard, select an audience you would like to configure the syncing, then Settings > Webhooks.

When creating a webhook check a checkbox Profile updates.

create a webhook in Mailchimp
You can also check “Email changed” if you would like to update user emails, so we may have a single webhook for multiple events.

Update user profiles automatically

In case you do not know where to insert the below code, I recommend you this guide.

add_action( 'template_redirect', 'misha_user_update_webhook' );

function misha_user_update_webhook(){

	// exit the function in case it is not our hook
	if( empty( $_GET[ 'process-webhook' ] ) || 'anything123' !== $_GET[ 'delete-users' ] ) {
		return;
	}

	// check the action
	if( empty( $_POST[ 'type' ] ) || 'profile' !== $_POST[ 'type' ] ) {
		return;
	}

	// we can check the list ID to improve the security of a webhook
	// if( empty( $_POST[ 'data' ][ 'list_id' ] ) || 'abcdef12345' !== $_POST[ 'data' ][ 'list_id' ] ) {
	// 	return;
	// }

	// do the thing
	$user = get_user_by( 'email', $_POST[ 'data' ][ 'email' ] );
	if( $user ) {

		wp_update_user( 
			array( 
				'ID' => $user->ID, 
				'first_name' => $_POST[ 'data' ][ 'merges' ][ 'FNAME' ],
				'last_name' => $_POST[ 'data' ][ 'merges' ][ 'LNAME' ],
				'display_name' => $_POST[ 'data' ][ 'merges' ][ 'FNAME' ] . ' ' . $_POST[ 'data' ][ 'merges' ][ 'LNAME' ] 
			)
		);

		// you can also use update_post_meta()
		// update_user_meta( $user->ID, 'first_name', $_POST[ 'data' ][ 'merges' ][ 'FNAME' ] );
				
	}
	exit;

}
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 Twitter