Update user’s first_name, last_name and email on a WordPress website when the appropriate MailChimp data has been changed
If you are interested in an opposite process – how to sync MailChimp lists with changes on your website, I recommend to look at my plugin.
Creating a webhook
First of all go to your MailChimp dashboard to create a webhook that will be fired when somebody has updated his profile or changed his email (check checkboxes Profile updates and Email changed).

As you can see, we have a single webhook for both events. If this step seems unclear for you, or maybe you would like to create a webhook with MailChimp API, I recommend to check this tutorial first.
Update a user on your WordPress website
Even if you are not so familiar with the code, no problems – all you have to do is to insert this ready-to-use piece of code to your (child) theme functions.php
. Just in case you changed the webhook URL above, do it here as well.
add_action( 'init', 'misha_profile_and_email_update_webhook');
function misha_profile_and_email_update_webhook(){
// exit the function in case it is not our hook
if( empty( $_GET['process-webhook'] ) || empty( $_POST['type'] ) || $_GET['process-webhook'] != 'anything' )
return;
$event_type = $_POST['type'];
switch( $event_type ) :
case 'profile':
// we need it because we have to get a User ID somehow
$user = get_user_by( 'email', $_POST['data']['email'] );
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() by the wa
//update_user_meta( $user->ID, 'first_name', $_POST['data']['merges']['FNAME'] );
die;
case 'upemail':
$user = get_user_by( 'email', $_POST['data']['old_email'] );
wp_update_user( array(
'ID' => $user->ID,
'user_email' => $_POST['data']['new_email']
) );
die;
endswitch;
}
I replaced break
with die
on lines 27 and 37 because we do not have to do anything else (load a website page 😱) once the appropriated to a certain webhook code is fired.

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
Hi,
The whole posts serie on Mailchimp Webhooks and API 3 is exactly what I was looking for and really didactic. Great job!!
However one specific question:
Everything’s fine if Mailchimp user change fname/lname OR email.
Unfortunatly if both fields are update together, profile AND upemail webhooks must be processed.
No problem if profile webhook is first then upemail webhook.
But if upemail webhook start first, user’s email is change and profile webhook can’t get the userID by email.
Any tips or trick to resolve this?
Thanks ;-)
Hey,
While updating from
upemail
I recommend to store an old email somewhere in user meta. So, when the profile webhook will be fired, check if user exists, if it doesn’t exist, try to find the user by a email we stored in user meta 🙃Correct answer but I’ve just realised that my question was completely wrong (in fact it’s just the opposite problem :D )
When modifying both email and fname/lname,
profile
is send with the new email and no reference to old one. So ifupemail
has not already been fired first to update the old email,profile
is unable to find any reference of the new email.The only solution I could find up to now is to resend
profile
datas after theupemail
with something like this:But this seems to me to be an ugly process…
I got lost, 😁
Let’s try from the beginning – where you modify email and first/last names? On the website?
P.S. You can use
<code>
tag in comments 🙃Sorry, I start myself to loose my mind with this full 2 way sync.
When a Mailchimp user updates his email and first/last names at the same time, 2 webhooks are sent,
profile
andupemail
.upemail
contains old and new emails andprofile
contains new first/last names and new email.My webhook listener is not always receiving both webhooks in same order.
Good case : If
upemail
is fired first, my webhook listener can updates my user email first so whenprofile
is then fired, the listener can find the related user with the new email.Bad case : If
profile
is fired first, the webhook listener is unable to find the related user as it is still using the old email while theprofile
webhook contains only the new email. Theget_user_by( 'email', $_POST['data']['email'] );
is not returning any valid WP_user object.My new idea to resolve this bad case is, when receiving the
profile
webhook to create a temporary user with new data fromprofile
and adding atemporary
user meta (thanks for the idea).So after when receiving
upemail
, I can first check if I can find a user with new email and thetemporary
user meta (meaning thatprofile
has been fired first) and getting new first/last names from this user before deleting him.Not a completely clean solution (a lot of efforts for basic result) but at least I get rid of the
wp_schedule_single_event
I didn’t trust so much.Thanks for time and posts serie!
Now that I have my Mailchimp to WordPress sync, I will try to set up the other way sync…
Sebastien,
This solution #comment-4188 should fit perfectly for what you’ve just described.
Sorry, I must be dumb.
What would be the use of storing the old email when Upemail as Profile webhook only contains new email?
Just when
upemail
is fired, useupdate_user_meta()
to store the old email somewhere in user meta.When
profile
is fired afterupemail
check if there is a user with a email passed in the hook. If there is not such user, get it by the specific meta key you stored the old email into.