Send notification when user role is changed
I have a client that want to manually verify a newly registered user data. When the user is registered in the website, it automatically assign the user as Pending
role. After verification, admin will manually change the user’s role at the backend User Page. Once the user’s role changed to ‘Active‘, a verification passed notification email will send to user.
Here is how I handle the situation:
add_action('profile_update','role_change_notify',10,3);
function role_change_notify($user_id,$old_user_data,$userdata) {
foreach($old_user_data->roles as $role):
$old_role = $role; //get the registered role. Should be 1 role since the user is newly registered.
endforeach;
$role_check = array('active','new');//the roles you wish to send notification. This will check the role of 'active' and 'new'
if($old_role == 'pending' && in_array($userdata['role'],$role_check) ):
//this if statement is to make sure it the following runs only the user is change from 'pending' role to 'active' or 'new'. So it prevent resend the notification whenever the user profile is updated.
$user_info = get_userdata( $user_id );
$to = $user_info->user_email;
$subject = "Your role is updated";
ob_start();
echo 'Your role is activated.'
$message = ob_get_clean();
wp_mail( $to, $subject, $message);
endif;
}
I am using the profile_update
hook for this condition.
Note that $role_check
part I using two roles active and new
in an array. This will send the notification if the role is changed to either active
or new
role. This is in case you have multiple roles where needed to send the notification to.
Before you want to use this snippet, make sure you already added the custom role like pending, active, or new. If you haven’t, you can use those role plugin to add it. Of course you can utilize the default’s WordPress role. It’s totally fine, you just need change the snippet accordingly.
UPDATE
The above snippet on works when you edit the admin profile page. If you want to send the notification whenever a user role is changed, regardless from which action (eg: change the role in the admin users table page). here is the snippet for it
add_action('set_user_role','user_on_role_change',10,3);
function user_on_role_change( $user_id, $new_role, $old_roles) {
// Getting role before update
foreach($old_roles as $role):
$old_role = $role;
endforeach;
$role_check = array('active','new');//the roles you wish to send notification. This will check the role of 'active' and 'new'
//If we change role send email
if($old_role == 'pending' && in_array($new_role,$role_check) ):
//this if statement is to make sure it the following runs only the user is change from 'pending' role to 'active' or 'new'. So it prevent resend the notification whenever the user profile is updated.
$user_info = get_userdata( $user_id );
$to = $user_info->user_email;
$subject = "Your role is approved";
ob_start();
echo 'Your role is activated.'
$message = ob_get_clean();
wp_mail( $to, $subject, $message);
endif;
}
In this snippet, I am using set_user_role
action instead. The function is pretty much the same, but note that the arguments are difference. In set_user_role
arguments, the user data is more straight forward. The new role can be obtain directly from the argument, unlike in update_profile
hook, you will need to access from the $userdata
data array.
françois
hi,
i’ve tryied your code but it seems not work. I want to use it for sending just a mail when a visitor which asked for ‘pro’ role is validated by the admin. visitor have already received the pending e-mail from another plugging.
Here the code i put in function.php of my child template. Did i did something wrong?
add_action(‘profile_update’,’role_change_notify’,10,3);
function role_change_notify($user_id,$old_user_data,$userdata) {
foreach($old_user_data->roles as $role):
$old_role = $role; //get the registered role. Should be 1 role since the user is newly registered.
endforeach;
$role_check = array(‘pro’);//the roles you wish to send notification. This will check the role of ‘pro’
if($old_role == ‘pending’ && in_array($userdata[‘role’],$role_check) ):
//this if statement is to make sure it the following runs only the user is change from ‘pending’ role to ‘pro’. So it prevent resend the notification whenever the user profile is updated.
$user_info = get_userdata( $user_id );
$to = $user_info->user_email;
$subject = “Votre nouveau compte Professionel”;
$message = ‘Bonjour, Félicitation. Votre compte professionnel est désormais accessible.
{customer_details}
L équipe So Damn Desserts’;
wp_mail( $to, $subject, $message);
endif;
}
Thanks
tck30
Are you sure the old role in here `if($old_role == ‘pending’ && in_array($userdata[‘role’],$role_check) )`:
is pending?
You said the user creation is from a plugin, you might need to check on it.