Skip to content

How To Send Email Notifications When User Role Changes In WordPress

Few weeks back, I shared a simple script which sends email notifications to users when someone or they update their profile. Here’s a simple script which sends email notifications when user role changes in WordPress. It’s a great way to notify your site’s users about role changes.

It’s must-use snippet if you’re running a WordPress installation with bbPress and/or BuddyPress community plugins. Just drop the following snippet to your current theme’s functions.php file:

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

Leave a Reply

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