It is easy to email users after their role has been changed

Changing user roles couldn’t be easier. Once you navigate to All Users and find one who needs a new role, you can simply select one from the dropdown list. After you save those changes, new user role activates and that user can do more (or less) on your WordPress website, depending on what you chose.

While there are six predefined roles in WordPress, you can easily create new ones or only rename those already registered and thus easily control the way people behave on your WordPress powered website.

If you haven’t modified your theme already, user who has been given a new role wouldn’t get any notification about it. In that case, you would either have to tell a user that the role has been changed or a person would find out on their own by accessing the profile page.

If you have only several users on your site and you don’t change the roles frequently, this probably doesn’t bother you. But if there are dozens or more users involved and you do change roles more frequently, you can use this function in order to send automatic email every time a role is changed:

  1. Open functions.php file
  2. Copy and paste the following function:
  3. 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);
    
  4. Rewrite email subject and message variables if you want to
  5. Save changes
  6. After you have made the changes, you won’t have to worry about notifying your users ever again. The function will automatically take their email addresses directly from profiles and send them a message with the subject and the message body you have specified in the function above.

    You don’t have to stop there; after you are done with installing this neat feature in your theme, you can also easily show specific content only to specific user role, redirect different user roles to different destination pages or even automatically email authors after you publish their articles.

START YOUR OWN BLOG

This guide is an introduction to mastering the art of blogging. It provides easy to follow steps to start, maintain, and grow your blog.

Read the guide

Leave a Reply

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