Wordpress:当用户注册时发送通知,但仅适用于具有订阅者角色的用户

Posted

技术标签:

【中文标题】Wordpress:当用户注册时发送通知,但仅适用于具有订阅者角色的用户【英文标题】:Wordpress: Send a notification when a user registers, but only for users with Subscriber role 【发布时间】:2021-08-15 04:35:36 【问题描述】:

我正在尝试在有人注册角色订阅者时让我的网站向我发送一封通知电子邮件。我可能可以通过 mu-plugins 上的钩子来实现这一点,但我不知道从哪里开始或使用哪个钩子。尝试在插件中添加 if 语句,但安装插件并仅针对此功能对其进行修改可能有点矫枉过正。谢谢!

【问题讨论】:

【参考方案1】:

为了您的信息,默认注册用户角色是“订阅者”角色,如果您设置了其他角色,您必须在注册后更改角色。 试试这个:

 function send_welcome_email_to_new_user($user_id) 
            $user = get_userdata($user_id);
            $user_email = $user->user_email;

            // email will send only for "Subscriber" registers
            if ( in_array( 'subscriber', $user->roles )) 
              $to = $user_email;
              $subject = "Hi";
              $body = '
                        <p>your message </p>
              ';
              $headers = array('Content-Type: text/html; charset=UTF-8');
              if (wp_mail($to, $subject, $body, $headers)) 
                error_log("email has been successfully sent to user whose email is " . $user_email);
              
            

            // email will send only for "Other Role" registers
            if ( in_array( 'other_role', $user->roles )) 
              $to = $user_email;
              $subject = "Hi";
              $body = '
                        <p>your message </p>
              ';
              $headers = array('Content-Type: text/html; charset=UTF-8');
              if (wp_mail($to, $subject, $body, $headers)) 
                error_log("email has been successfully sent to user whose email is " . $user_email);
              
            

            
            add_action('user_register', 'send_welcome_email_to_new_user');

【讨论】:

【参考方案2】:

它在 WordPress 中默认启用。检查 WordPress 是否正在发送电子邮件,您的 SMTP 设置可能有问题,您需要配置 SMTP。您的网站托管在哪里?你检查过垃圾邮件吗?

【讨论】:

关键是只有当客户注册了该角色时,我才需要发送电子邮件。谢谢!

以上是关于Wordpress:当用户注册时发送通知,但仅适用于具有订阅者角色的用户的主要内容,如果未能解决你的问题,请参考以下文章

使用 Gmail API 发送的邮件中缺少附件,但仅适用于收件人

WordPress Gravity Forms 按用户角色向 WP 用户动态发送通知

当用户不打开应用程序时如何发送通知?

当用户到达特定位置时向用户发送推送通知 [IOS]

pipenv install --system 但仅适用于用户

当应用程序在后台时向用户发送通知