Home / Archive / MemberPress: Send Email to User When Their User Role Is Changed
Duplicate Snippet

Embed Snippet on Your Site

MemberPress: Send Email to User When Their User Role Is Changed

Adding this code snippet to a website will send an automated email to the user each time their WordPress user role is changed.

The subject of the email that will be sent with the example code is: “Your role has changed: yourdomain.com”. You can modify this by changing the Your role has changed: text on the following line:

$subject = "Your role has changed: ".$site_url; // Email Subject

Further, the content (body) of the email in this example will look like this: Hello [User’s name], your role has changed on yourdomain.com, congratulations you are now an [New User Role].

You can modify the email body by adjusting text on this line:

$message = "Hello " .$user_info->display_name . ", your role has changed on ".$site_url.", congratulations you are now an " . $new_role; //Email Body

Code Preview
php
<?php
function mepr_email_user_role_change( $user_id, $new_role ) {
  $site_url = get_bloginfo( 'wpurl' );
  $user_info = get_userdata( $user_id );
  $to = $user_info->user_email;
  $subject = "Your role has changed: ".$site_url; // Email Subject
  $message = "Hello " .$user_info->display_name . ", your role has changed on ".$site_url.", congratulations you are now an " . $new_role; //Email Body
  wp_mail($to, $subject, $message);
}
add_action( 'set_user_role', 'mepr_email_user_role_change', 10, 2 );

Comments

Add a Comment