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

Wordpress: Send a notification when a user registers, but only for users with Subscriber role

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

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

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

 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');