wordpress snippets last updated   : July 14, 2014
we now have 634 snippets

Send email notification when user role changes

wordpress snippet

Adding this snippet to the functions.php of your wordpress theme will send the member an email notification when the user’s role has changed.

snippet :  PHPcopy
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);
source →
  • http://raamdev.com Raam Dev

    Brilliant! Thanks!

    • http://wpsnipp.com Kevin Chard

      Thanks Raam, glad to hear you like it.

  • Pontus

    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).

    • Tiago Dufau de Vargas

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

  • Tiago Dufau de Vargas

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

  • Mariusz Szatkowski

    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

  • Mariusz Szatkowski

    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

  • Matt

    This was super helpful! Thank you!

jqsnipp.com | we just launched jquery snippets website