Send Email Notification to User on Profile Updates

How to Send Email Notification to User on Profile Updates

in Optimization & Best Practice
Google+ LinkedIn

On a project, I had a request from client who wants WordPress users to be notified by email when they update information in their user profile. I think its fair because most of the websites sends an automatic email notification when a user updates his profile and it’s good for security reasons too. But the small problem is that WordPress by default does not have such feature or an option in admin panel, but lucky WordPress provides a range of action hooks and filters that executes while WordPress perform any action. In this particular case, I used WordPress action profile_update to trigger email notifications. It’s also important to note that by default WordPress uses “From Email” as “[email protected]”, so you might want to change it to your contact email address too.

To send email notification, we will be using WordPress mail function wp_mail and we will trigger it by action when a user profile information is updated, whether by user itself or by an admin. So following is the simple code snippet to send email notification to user. We can modify this snippet to inform website admin too but we will cover that in future articles. In this snippet I have added a sample profile update message which you should change if you want to provide more information about changed information or how to contact support if it was a mistake.

By pasting this code snippet in your theme’s functions.php file, WordPress will automatically send email notification on any profile updates.

// send email notification to user on profile updates
function wcs_user_profile_update_notification( $user_id ) {
    $site_url = get_bloginfo( 'wpurl' );
    $site_title = get_bloginfo( 'name' );
    $user_info = get_userdata( $user_id );
    $to = $user_info->user_email;
    $to_name = $user_info->display_name;
    $subject = "User Profile Updated: " . $site_url;

    // message body
    $message = "Hello " . $to_name . ",\n\n";
    $message .= "Your profile has been updated!\n\n";
    $message .= $site_title . "\n";
    $message .= $site_url;

    wp_mail( $to, $subject, $message );
}
add_action( 'profile_update', 'wcs_user_profile_update_notification', 10, 2);

Share the love

If you like this snippet, share it with others!

Leave a Reply

Your email address will not be published. Required fields are marked *