How to Send Email Notifications in WordPress When a User Profile Updates

Home » Snippets » How to Send Email Notifications in WordPress When a User Profile Updates
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

Are you interested in tracking changes made to user profiles on your WordPress site? Or perhaps, you simply want to stay up-to-date when a profile is updated. Using an email notification system can be a practical solution for both. This article will guide you, step by step, on how to send an email notification anytime a user profile gets updated on your WordPress page.

					function wpturbo_send_profile_update_notification($user_id, $old_user_data) {
    // Get the current user data
    $user = get_userdata($user_id);

    // Create the email content
    $message = sprintf(__('Dear %s,', 'wpturbo'), $user->first_name);
    $message .= "rn";
    $message .= __('This is a notification to let you know that your profile details on our website have been updated.', 'wpturbo');
    $message .= "rn";

    // Send the email
    wp_mail($user->user_email, __('Profile Update Notification', 'wpturbo'), $message);
}
add_action('profile_update', 'wpturbo_send_profile_update_notification', 10, 2);
				

The PHP function wpturbo_send_profile_update_notification() as defined in the snippet above is activated when a user profile is updated on the WordPress site. This function sends an automated email notification to the user to inform them about the changes.

This function takes two parameters: $user_id and $old_user_data. The $user_id is the unique identifier of the user, while $old_user_data contains the previous user data prior to the changes.

The first step in the function involves retrieving the current user’s data with the WordPress get_userdata() function by passing the $user_id as its parameter.

$user = get_userdata($user_id);

The $user object will now contain all the user details. This allows us to personalise the email and also determine which email address to send it to.

Next, we compose the body of the email using the sprintf() function, which incorporates a placeholder for the first name that will be replaced by the user’s actual first name.

$message = sprintf(__('Dear %s,', 'wpturbo'), $user->first_name);

The rn character is a way to make a new line in the email text. We use it to append more content to the message string, including letting the user know that their profile has been updated.

The __('This is a notification to let you know that your profile details on our website have been updated.', 'wpturbo'); line is written to be translatable, encompassing multiple languages if needed.

$message .= "rn";
$message .= __('This is a notification to let you know that your profile details on our website have been updated.', 'wpturbo');
$message .= "rn";

The next step is to send the email using the wp_mail() function. The function’s parameters include the recipient’s email address, the email subject, and the email body or message. We extract the recipient’s email from the user’s data, set a subject, and use the message we composed earlier.

wp_mail($user->user_email, __('Profile Update Notification', 'wpturbo'), $message);

The hook to the profile_update action is the final step, which activates the wpturbo_send_profile_update_notification() function whenever a user’s profile is updated.

The 10 and 2 following the function name in the add_action function represent the priority and accepted arguments respectively. With a priority of 10, we are ensuring our function runs after WordPress’s inbuilt functions related to profile updates. The 2 allows our function to accept two arguments – $user_id and $old_user_data.

add_action('profile_update', 'wpturbo_send_profile_update_notification', 10, 2);

Overall, this function creates an automated email that is sent to users when their profile details have been updated, giving them timely information about changes to their account.

Register an account to save your snippets or go Pro to get more features.