WordPress / Web Development Tutorials
(Best WordPress Tutorials)

CSSHTMLJavaScriptjQueryMySQLPHPSilvaTechnologiesWooCommerceWordpress
Silva Web Designs - Blog

Notify a WordPress User when their Role has Been Changed to a Specific Role

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!

 

Nathan da Silva - Profile

Posted by: Nathan da Silva

Nathan is the Founder of Silva Web Designs. He is passionate about web development, website design and basically anything digital-related. His main expertise is with WordPress and various other CMS frameworks. If you need responsive design, SEO, speed optimisation or anything else in the world of digital, you can contact Silva Web Designs here; [email protected]

It’s good to share

Join the discussion