根据用户角色在注册时发送不同的电子邮件通知

时间:2017-04-23 作者:rasel mahmud

我有一个网站,有两个名为“a”和“B”的用户角色。当任何人在“a”注册时,我想发送一封欢迎电子邮件,当“B”想发送不同的欢迎电子邮件时,是否可能?提前感谢

2 个回复
SO网友:Jared Cobb

是的,这是可能的。WordPress定义了一个名为wp_new_user_notification. 您可以通过创建自己的版本(也称为wp_new_user_notification) 在您自己的主题或插件中。

您可能希望从复制contents of the existing core version 输入您自己的副本。现在,您可以根据自己的需要自定义逻辑。

由于此函数通过$user_id 作为第一个参数,您可以使用get_userdata 获取有关用户的更多信息(例如,该用户的角色),然后根据其角色发送不同的内容。

$user_data = get_userdata( $user_id );
if ( ! empty( $user_data->roles ) ) {
    if ( in_array( \'role_a\', $user_data->roles, true ) {
        // custom content for role a
    } elseif ( in_array( \'role_b\', $user_data->roles, true ) {
        // custom content for role b
    }
}

SO网友:rasel mahmud

我已经解决了这个问题。谢谢你的大力帮助@jared cobb

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

            // email will send only for "A" registers
            if ( in_array( \'A\', $user->roles )) {
              $to = $user_email;
              $subject = "Hi A, welcome to our site!";
              $body = \'
                        <h1>Dear A,</h1></br>
                        <p>Thank you for joining our site. Your account is now active.</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 "B" registers
            if ( in_array( \'B\', $user->roles )) {
              $to = $user_email;
              $subject = "Hi B, welcome to our site!";
              $body = \'
                        <h1>Dear B,</h1></br>
                        <p>Thank you for joining our site. Your account is now active.</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\');

相关推荐

更改wp-admin/plugins.php上统计的插件数量

我已成功地使用从插件页面隐藏我的插件$wp_list_table 然而,顶部的分页仍然将插件列为“所有(3)”等。我成功地改变了$wp_list_table 的数组_pagination_args = total_items.但它仍然在页面顶部呈现插件-“全部(3)”。有什么办法可以解决这个问题吗?我找到了WP_Plugins_List_Table::prepare_items()具有全局$totals 变量,但我不确定我将如何改变这一点,在这个函数中$totals = array(); fore