X

How to Send Email Notification When User Role Changes in WordPress

Snippets by IsItWP

Are you looking for a way to send email notifications to WordPress users when their user role changes? While there’s probably a plugin for this, we have created a quick code snippet that you can use to send email notifications to members when their user role has changed in WordPress.

Instructions:

All you have to do is add this code to your theme’s functions.php file or in a site-specific plugin:

function user_role_update( $user_id, $new_role ) {
        $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);

Note: If this is your first time adding code snippets in WordPress, then please refer to our guide on how to properly copy / paste code snippets in WordPress, so you don’t accidentally break your site.

If you liked this code snippet, please consider checking out our other articles on the site like: 62 best free WordPress blog themes and 7 best WordPress contact form plugins.

Comments   Leave a Reply

  1. how do I use HTML tags here?
    I want to make texte bold
    $message = “Hello ” .$user_info->display_name . ” your role has changed on “.$site_url.”, congratulations you are now an ” . $new_role;

    1. You could try using this code for that line:

      $message = “Hello ” .$user_info->display_name . ” your role has changed on “.$site_url.”, congratulations you are now an ” . $new_role . ““;

  2. Perfect đź‘Ť
    Thank you so much đź‘Ź

  3. You article has help me but it also sends email to new registrations. I want to use your code for two roles that I want custom messages for. Would this code work for that?

    1. The code would need to be modified to check the value of $new_role.

  4. I used this code but no mail received when I change my role

  5. this one works and does not send email if new register

    function user_role_update( $user_id, $new_role ) {

    if ($new_role == “customer”) {}

    elseif ($new_role == “role1″||”role2”) {

    $site_url = get_bloginfo(‘wpurl’);
    $user_info = get_userdata( $user_id );
    $to = $user_info->user_email;
    $subject = “A sua conta foi validada. Pode agora começar a comprar em 1223.com”;
    $message = “welcome ” .$user_info->display_name . “! message .” ;
    wp_mail($to, $subject, $message);
    }}

    add_action( ‘set_user_role’, ‘user_role_update’, 10, 2);

  6. This was super helpful! Thank you!

  7. Mariusz Szatkowski October 31, 2013 at 6:59 am

    Hi,

    1. Thanks for the snippet – but I want to notify user only when the role of existing user is changed.

    Now the script sends an email when new user is added to WP.

    2. It would be more accurate to send the role name (now it sends the slug)

    Any ideas?

    Thanks

    1. Hi did you received the snippet? if possible could you share me the link too? cause im facing the same error of not sending a separate email

  8. Mariusz Szatkowski September 18, 2013 at 1:56 am

    Hi, thanks for that snippet. What about sending notification when the role is changed to certain name?

    Eg. I want to send the notification when the user role is changed to ‘group-x’…

    Thanks and kind regards
    M

  9. Tiago Dufau de Vargas September 6, 2013 at 3:43 pm

    Saved me a day of research!! Thanks a lot.

  10. Any idea how to solve this if you want the email to be sent only when you change their role, not when you set the role? With this snippet, an email will be sent when you add a new user. An email will also be sent when you update e.g a users firstname (not touching the role).

    1. Tiago Dufau de Vargas September 6, 2013 at 3:46 pm

      You could try to use get_userdata and verify if the current user role is different than the new one…

  11. Brilliant! Thanks!

    1. Thanks Raam, glad to hear you like it.

Add a Comment Cancel reply

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!