How to Send Email Notifications in WordPress when User Roles Change

Home » Snippets » How to Send Email Notifications in WordPress when User Roles Change
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

Managing a WordPress website with multiple users can be a challenging job. From site administrators to contributors, keeping track of role assignments can sometimes slip through the cracks. Imagine if you could be immediately notified whenever a user role changes on your site, sounds convenient, right? In this article, we will guide you through a streamlined process of setting up automatic email notifications for whenever a user role changes on your WordPress site. This will ensure that you are always on top of user management, promoting efficient website administration.

					function wpturbo_send_email_on_role_change( $user_id, $new_role ) {
    $user = get_userdata( $user_id );
    $email = $user->user_email;
    $subject = 'Your role has changed on ' . get_bloginfo('name');
    $message = 'Hello ' . $user->display_name . ', your role has changed to ' . $new_role;

    wp_mail($email, $subject, $message);
}

add_action( 'set_user_role', 'wpturbo_send_email_on_role_change', 10, 2);
				

The code snippet begins by declaring a new function named ‘wpturbo_send_email_on_role_change’. This function is to be called whenever a user’s role in the WordPress site changes. It accepts two parameters: $user_id and $new_role. $user_id will be the unique ID of the user whose role is changing, and $new_role will be the name of the new role that the user is assuming.

function wpturbo_send_email_on_role_change( $user_id, $new_role ) {

Inside the function, we first call the WordPress function get_userdata($user_id), which retrieves all the relevant information about the user with the respective ID and returns it as an object. We store this object in the $user variable.

    $user = get_userdata( $user_id );

From the $user object, we can access the user’s email address stored in the user_email property. We store this email in the $email variable, which will serve as the recipient’s email address.

    $email = $user->user_email;

We then define a $subject and a $message for our email. The $subject of the email is a string that states ‘Your role has changed on’ followed by the name of our blog, retrieved using the WordPress function get_bloginfo('name').

    $subject = 'Your role has changed on ' . get_bloginfo('name');

The $message of our email greets the user by their display name and informs them of their new role:

    $message = 'Hello ' . $user->display_name . ', your role has changed to ' . $new_role;

Finally, we send the email using WordPress’s built-in wp_mail() function. We pass the recipient’s email address, the email subject, and the email message to the wp_mail() function as parameters:

    wp_mail($email, $subject, $message);
}

The last line of the code hooks our wpturbo_send_email_on_role_change() function into WordPress’s set_user_role action, passing along the user’s ID and their new role as arguments. This line ensures that our function is triggered, and an email notification is sent, every time a user’s role changes on the site:

add_action( 'set_user_role', 'wpturbo_send_email_on_role_change', 10, 2);

The ‘10’ passed as a third argument to the add_action() function dictates the priority of our function in relation to others hooked into the set_user_role action – this could be changed to make our function run earlier or later. The ‘2’ indicates the number of arguments that the wpturbo_send_email_on_role_change function accepts, which are the $user_id and $new_role.

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