How to Send Email Notification When User Role Changes in WordPress




Geniuswp show

Summary: Last updated on January 28th, 2019 by Debjit Saha 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. gioSeptember 16, 2021 at 9:10 pmhow 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;Reply James P.September 20, 2021 at 3:58 pmYou 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 . ““;Reply EmreJune 19, 2021 at 9:09 pmPerfect