关闭新用户注册电子邮件

时间:2016-08-16 作者:rudtek

我想关掉管理员在新用户注册时收到的电子邮件。由于新注册(它们是合法注册),我们收到了很多电子邮件,但我不想看到电子邮件告诉我有人一次又一次注册了。

到目前为止我已经试过了installing plugins 但它们不起作用。(支持部门甚至表示,它们已经不起作用了)。在这里搜索时,我能找到的唯一问题是Turn off admin emails for new user registrations这是三年前的事,似乎也不管用。我尝试了以下代码:

// Redefine user notification function
if ( !function_exists(\'wp_new_user_notification\') ) {
function wp_new_user_notification( $user_id, $plaintext_pass = \'\' ) {
    $user = new WP_User($user_id);

    $user_login = stripslashes($user->user_login);
    $user_email = stripslashes($user->user_email);

    $message  = sprintf(__(\'New user registration on your blog %s:\'), get_option(\'blogname\')) . "rnrn";
    $message .= sprintf(__(\'Username: %s\'), $user_login) . "rnrn";
    $message .= sprintf(__(\'E-mail: %s\'), $user_email) . "rn";


      //  @wp_mail(get_option(\'admin_email\'), sprintf(__(\'[%s] New User Registration\'), get_option(\'blogname\')), $message);

        if ( empty($plaintext_pass) )
            return;

        $message  = __(\'Hi there,\') . "rnrn";
        $message .= sprintf(__("Welcome to %s! Here\'s how to log in:"), get_option(\'blogname\')) . "rnrn";
        $message .= wp_login_url() . "rn";
        $message .= sprintf(__(\'Username: %s\'), $user_login) . "rn";
        $message .= sprintf(__(\'Password: %s\'), $plaintext_pass) . "rnrn";
        $message .= sprintf(__(\'If you have any problems, please contact me at %s.\'), get_option(\'admin_email\')) . "rnrn";
        $message .= __(\'Adios!\');

        wp_mail($user_email, sprintf(__(\'[%s] Your username and password\'), get_option(\'blogname\')), $message);

    }
}
我的主题功能。php(它有一行@wp\\u mail注释掉了),我甚至试着在wp-includes/pluggable.php 但我还是收到了电子邮件。

我现在正在使用WordPress 4.5.3。(作为本流程的一部分,更新至4.6)

我想尽量说清楚。我不希望发送给用户的电子邮件停止,因为他们仍然应该收到,但我如何才能使发送给管理员的电子邮件停止?

3 个回复
最合适的回答,由SO网友:bosco 整理而成

Generic Pluggable Approach for WordPress < 4.6 (See @birgire\'s Answer for > 4.6)

可插拔功能是WordPress过去令人沮丧的遗物之一,并且伴随着一系列复杂的功能。直接修改核心文件(正如@Jarmerson在评论中提到的那样,这是完全不可取的)不起作用,这让我怀疑您安装的另一个插件可能正在覆盖可插拔文件。

这个wp-includes/pluggable.php 文件加载在活动插件和mu插件之后,但在活动主题之前;这意味着“可插入函数”只能由插件中的声明替代。

您在另一个答案中发现的修改适用于更旧版本的WordPress。在替换任何可插入功能的过程中,您应该从the original function as it exists in your installation\'s version (在您的例子中,是v4.5.3)。这样,解决方案如下所示(注释省略;未添加任何行,仅删除):

function wp_new_user_notification( $user_id, $deprecated = null, $notify = \'\' ) {
  if ( $deprecated !== null )
    _deprecated_argument( __FUNCTION__, \'4.3.1\' );

  if ( \'admin\' === $notify || ( empty( $deprecated ) && empty( $notify ) ) ) 
    return;

  global $wpdb, $wp_hasher;
  $user     = get_userdata( $user_id );
  $blogname = wp_specialchars_decode(get_option(\'blogname\'), ENT_QUOTES);
  $key      = wp_generate_password( 20, false );

  do_action( \'retrieve_password_key\', $user->user_login, $key );

  if ( empty( $wp_hasher ) ) {
    require_once ABSPATH . WPINC . \'/class-phpass.php\';
    $wp_hasher = new PasswordHash( 8, true );
  }

  $hashed = time() . \':\' . $wp_hasher->HashPassword( $key );
  $wpdb->update( $wpdb->users, array( \'user_activation_key\' => $hashed ), array( \'user_login\' => $user->user_login ) );

  $message = sprintf(__(\'Username: %s\'), $user->user_login) . "\\r\\n\\r\\n";
  $message .= __(\'To set your password, visit the following address:\') . "\\r\\n\\r\\n";
  $message .= \'<\' . network_site_url("wp-login.php?action=rp&key=$key&login=" . rawurlencode($user->user_login), \'login\') . ">\\r\\n\\r\\n";
  $message .= wp_login_url() . "\\r\\n";

  wp_mail($user->user_email, sprintf(__(\'[%s] Your username and password info\'), $blogname), $message);
}
我省略了传统的if( !function_exists() ) 检查是否通常封装了可插拔覆盖,因为在这种情况下,可能会出现重复声明错误-这将表明另一个插件已覆盖wp_new_user_notification() 功能,因此您这样做的尝试被完全忽略。

我建议将此函数放置在mu-plugin 因为它减少了另一个插件击败你的机会。在任何情况下,都不要修改核心文件wp-includes/pluggable.php 与上述内容。

SO网友:birgire

Approach for WordPress 4.6+

签出修补程序#36009 这被合并到WordPress版本4.6中。

信息技术adds 这个\'user\' 的选项$notify 的输入参数wp_new_user_notification(), 跳过向管理员发送这些电子邮件。

How it works

这个register_new_user() 函数包含以下部分:

do_action( \'register_new_user\', $user_id );
电子邮件通知通过以下方式激活:

add_action( \'register_new_user\', \'wp_send_new_user_notifications\' );
其中回调定义为:

function wp_send_new_user_notifications( $user_id, $notify = \'both\' ) {
        wp_new_user_notification( $user_id, null, $notify );
}

Workaround

因此,我们可以使用自定义回调尝试这种方法,并删除默认方法:

add_action( \'init\', function()
{
    remove_action( \'register_new_user\',   \'wp_send_new_user_notifications\'         );
    add_action(    \'register_new_user\',   \'wpse236122_send_new_user_notifications\' );
} );

function wpse236122_send_new_user_notifications(  $user_id, $notify = \'user\' )
{   
    wp_send_new_user_notifications( $user_id, $notify );    
}
更改默认值的位置\'both\'\'user\'.

值得一提的是wp_send_new_user_notifications() 还与以下操作相关联:

  • network_site_new_created_user
  • network_site_users_created_user
  • network_user_new_created_user
  • edit_user_created_user
我们可以用与上述类似的方式处理这些问题。

SO网友:Jarod Thornton

After some research I determined that you can disable all notification emails, but not those that send to admin.

根据您最初的问题,我可以就如何处理WordPress用户注册和密码重置电子邮件中不需要的通知电子邮件提供一些建议。

假设您有一个运行安装的cPanel环境,只需执行以下步骤,您就可以有效地删除这些特定的电子邮件。

通道Account Level Filtering 来自cPanel管理员。在此区域中,您可以管理主帐户的筛选器。你想Create Filter 并继续创建New Filter 对于All Mail on Your Account.

Rule 字段选择Toequals. 注意:您还可以为其他条件创建过滤器。最后一个字段是一个文本区域,您可以输入正在使用的电子邮件地址。低于rules 区域是魔法发生的地方。选择Discard Message. 还有其他选择。

我总是创建一个我知道不会使用的电子邮件地址,并使用它创建过滤器。

我可以继续谈论WordPress可插入挂钩之类的东西,但没有什么能完全按照您的意愿来做。真倒霉

相关推荐

WP-ADMIN:“对不起,您不能访问此页面。”

两周后不使用我们的WP站点,登录/wp-admin/ 出现白屏错误:抱歉,不允许您访问此页面。我有:重命名plugins 到plugins.temp.通过phpMyAdmin创建了一个新的管理员用户。新用户已user_meta 的作用a:1:{s:13:"administrator";s:1:"1";}.</已重命名themes 到themes.temp.</删除核心WordPress文件,并替换为新上载的文件</已替换.htaccess 使用默认