Do you want to send an email on user role change in WordPress? Then you have come to the right place.
Let’s say you want to notify a user if their role has been changed from Subscriber to Contributor, we can do this using the below code:-
function user_role_update( $user_id, $new_role ) {
if ($new_role == 'contributor') {
$site_url = get_bloginfo('wpurl');
$user_info = get_userdata( $user_id );
$to = $user_info->user_email;
$subject = "Role changed: ".$site_url."";
$message = "Hello " .$user_info->display_name . " your role has changed on ".$site_url.", congratulations you are now an " . $new_role;
wp_mail($to, $subject, $message);
}
}
add_action( 'set_user_role', 'user_role_update', 10, 2);
This will also work with newly created roles you have added. I tend to use Advanced Access Manager plugin to add newly created roles as it gives more flexibility in their permissions.
In my case, I wanted to send out an automated email to a WordPress user once their account has been approved. Using the above code I was able to achieve exactly what I wanted.
I hope this helps!