You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WordPress中Pro角色审批通过后邮件通知未送达问题排查

问题:用户从pending角色升级为pro时未收到邮件通知

我希望在管理员批准申请“Pro”角色的用户时发送邮件通知。需说明的是,该用户已通过其他插件收到待审核状态的邮件通知。以下是我编写的代码:

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;
}

但用户并未收到确认邮件通知,请问可能是什么原因?


可能的原因及排查方向

  • 旧角色获取逻辑有缺陷:代码通过foreach循环取旧角色数组的最后一个值作为$old_role,如果用户存在多个旧角色(即使你认为新注册用户只有一个,也可能因插件操作导致多角色),会覆盖正确的待审核角色。应直接判断pending是否在旧角色数组中,而非依赖单个变量。修改示例:

    // 替换原条件判断
    if (in_array('pending', $old_user_data->roles) && in_array('pro', get_userdata($user_id)->roles))
    
  • 新角色取值不准确:$userdata['role']是更新时提交的表单数据,不一定等于用户最终的角色(可能被其他插件修改)。应通过get_userdata($user_id)获取更新后的用户对象,再提取角色数组进行判断。

  • wp_mail函数执行异常:

    • 检查WordPress邮件配置:确认SMTP插件或系统默认邮件设置正常,可先执行一段简单的wp_mail测试代码验证发送功能。
    • 邮件内容占位符未替换:{customer_details}是未处理的占位符,部分邮件系统可能因此拦截邮件,或用户收到不完整内容误以为未收到。
    • 检查垃圾邮件文件夹:邮件可能被归类为垃圾邮件,需提醒用户查看。
  • 角色名称不匹配:确认旧角色的实际名称是否为pending(可能存在大小写差异,或插件使用类似pending_pro的自定义角色),可临时添加var_dump($old_user_data->roles)输出旧角色数组,验证名称是否正确。

  • 钩子执行顺序问题:若有其他插件也监听profile_update钩子且优先级更高,可能在你的代码执行前修改了角色状态,导致条件判断不成立。可尝试提高钩子优先级(比如把10改成20),确保代码在其他插件之后执行。


内容的提问来源于stack exchange,提问作者françois gilles

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.13 20:03:23