最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Sending an email notification when a user role is approved in Wordpress - Stack Overflow

programmeradmin5浏览0评论

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 Please edit your question to specifically and clearly define the problem that you are trying to solve. Additional details — including error messages and debugging logs — will help readers to better understand your problem and what you are asking. – mmm Commented Mar 21 at 12:54
  • try to debug your code. what I could find is maybe $role_check = array('pro') should be $role_check = 'pro' – IT goldman Commented Mar 21 at 14:07
  • @ITgoldman $role_check needs to be an array since it's used in the in_array() function in the if-statement. – geertjanknapen Commented Mar 21 at 14:20
  • You need to be sure $old_role will be 'pending', otherwise the code in the if-statement will never be executed. – geertjanknapen Commented Mar 21 at 14:21
  • "but i don't receive the confirmation e-mail" - just to state what is hopefully obvious, but before sending an email I would make sure the code's logic is working, and you can do that with a simple die('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
Add a comment  | 

1 Answer 1

Reset to default 1

There 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 );
发布评论

评论列表(0)

  1. 暂无评论