I try to send an email notification when a user which asked for "pro" role is validated by the admin.
Note that the user has already received a pending email notification from another plugin.
Here is my code:
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'
//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.
if( $old_role == 'pending' && in_array($userdata['role'],$role_check ) ):
$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';
wp_mail($to, $subject, $message);
endif;
}
But the user doesn't receive any confirmation email notification. Any clue?
I try to send an email notification when a user which asked for "pro" role is validated by the admin.
Note that the user has already received a pending email notification from another plugin.
Here is my code:
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'
//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.
if( $old_role == 'pending' && in_array($userdata['role'],$role_check ) ):
$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';
wp_mail($to, $subject, $message);
endif;
}
But the user doesn't receive any confirmation email notification. Any clue?
Share Improve this question edited Mar 21 at 16:22 LoicTheAztec 255k24 gold badges399 silver badges446 bronze badges asked Mar 21 at 12:34 françois gillesfrançois gilles 21 5 |1 Answer
Reset to default 1There are multiple mistakes in your code and it can be simplified.
Note: You should always enable WP Debug when testing, to get error logs.
Try the following instead:
function role_change_notify( $user_id, $old_user_data ) {
$user_data = get_user_by( 'id', $user_id ); // Get current user data
// Check for previous 'pending' and current 'pro' user roles
if( in_array( 'pending', $old_user_data->roles ) && in_array( 'pro', $user_data->roles ) ) {
$to = $user_data->user_email;
$subject = "Votre nouveau compte Professionel";
$message = "Bonjour, félicitation ! Votre compte professionnel est désormais accessible.
{customer_details}
L'équipe";
wp_mail( $to, $subject, $message);
}
}
It should work.
Note:
I removed the 3rd function argument, as it's not really needed. You will use the function like:
role_change_notify( $user_id, $old_user_data );
$role_check = array('pro')
should be$role_check = 'pro'
– IT goldman Commented Mar 21 at 14:07$role_check
needs to be an array since it's used in thein_array()
function in the if-statement. – geertjanknapen Commented Mar 21 at 14:20$old_role
will be'pending'
, otherwise the code in the if-statement will never be executed. – geertjanknapen Commented Mar 21 at 14:21die('Role changed');
message. This way you aren't hunting for a bug that turns out to be unrelated to code but instead elsewhere in SMTP, SPF, DKIM, etc. – Chris Haas Commented Mar 21 at 15:34