关闭新用户注册的管理员电子邮件

时间:2013-03-11 作者:Rob

注册新用户时,如何关闭用户和管理员的电子邮件通知?

我已经看到了一些建议和插件,但似乎都不起作用。一个是从一个插件中获取功能:

if ( !function_exists(\'wp_new_user_notification\') ) :
/**
 * Notify the blog admin of a new user, normally via email.
 *
 * @since 2.0
 *
 * @param int $user_id User ID
 * @param string $plaintext_pass Optional. The user\'s plaintext password
 */
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);

    // The blogname option is escaped with esc_html on the way into the database in sanitize_option
    // we want to reverse this for the plain text arena of emails.
    $blogname = wp_specialchars_decode(get_option(\'blogname\'), ENT_QUOTES);

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

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

    if ( empty($plaintext_pass) )
        return;

    $message  = sprintf(__(\'Username: %s\'), $user_login) . "\\r\\n";
    $message .= sprintf(__(\'Password: %s\'), $plaintext_pass) . "\\r\\n";
    $message .= wp_login_url() . "\\r\\n";

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

}
endif;
这些问题和建议都很老了,所以WP 3.5可能有点过头了。

在注册时,我仍然会收到管理员电子邮件和发给用户的电子邮件。

但我不想阻止忘记密码的电子邮件。

4 个回复
SO网友:Eugene Manuilov

作用wp_new_user_notification 是可插拔的。这意味着您可以通过在插件/主题中声明此函数的版本来覆盖它。

因此,如果要完全禁用所有通知,请按以下方式执行:

if ( !function_exists( \'wp_new_user_notification\' ) ) :
function wp_new_user_notification( $user_id, $plaintext_pass = \'\' ) {
    return;
}
endif;
但是,我不建议您禁用所有通知,我建议您至少向用户发送通知(用户如何找到他的密码?)。因此,在这种情况下,您的代码应该如下所示:

if ( !function_exists( \'wp_new_user_notification\' ) ) :
function wp_new_user_notification( $user_id, $plaintext_pass = \'\' ) {
    $user = get_userdata( $user_id );

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

    $blogname = wp_specialchars_decode(get_option(\'blogname\'), ENT_QUOTES);

    if ( empty($plaintext_pass) ) {
        return;
    }

    $message  = sprintf(__(\'Username: %s\'), $user_login) . "\\r\\n";
    $message .= sprintf(__(\'Password: %s\'), $plaintext_pass) . "\\r\\n";
    $message .= wp_login_url() . "\\r\\n";

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

SO网友:cptstarling

根据this page 使用此代码即可(完全禁用所有通知):

if ( !function_exists(\'wp_new_user_notification\') ) {    
  function wp_new_user_notification( ) {}    
}
这与给出的答案非常接近,但略短一些。我想把这个分享给大家。

SO网友:McPace

从4.6开始,有一个参数用于禁用管理员通知。https://developer.wordpress.org/reference/functions/wp_new_user_notification/

自4.6.0以来$notify 参数接受“user”仅向创建的用户发送通知

  • @参数字符串$通知可选。应发生的通知类型。接受“admin”或空字符串(仅限admin)、“user”或“both”(admin和user)。默认为空*/
  • 函数wp\\u new\\u user\\u notification($user\\u id,$deprecated=null,$notify=\'user\')
    SO网友:user119030

    今天不得不这么做,发现其中许多解决方案似乎已经过时了。这看起来是一种不覆盖可插入函数的更好方法。这不是我的确切代码,但应该是一个很好的参考。

    add_action( \'register_post\', \'maybe_stop_notifications\', 10, 3 ); 
    
    function maybe_stop_notifications ( $sanitized_user_login, $user_email, $errors ) { 
    
      if( empty( $errors->get_error_code() )) { 
    
           remove_action( \'register_new_user\', \'wp_send_new_user_notifications\' );
    
      }
    } 
    

    结束

    相关推荐

    将admin-ajax.php添加到前端。好主意还是坏主意?

    我喜欢管理ajax。php。但我讨厌必须本地化才能将前端脚本指向它,我希望有一个等效的、易于查找的主题文件。(看到前端请求通过“/wp-admin/”,我也很烦恼。没有实际原因,只是看起来很难看。)所以我只是复制了管理ajax。php中的根目录“/ajax.php”,调整了包含路径并删除了WP\\u ADMIN常量定义。看起来就像gangbusters(我现在可以将所有前端AJAX请求直接发送到/AJAX.php!并且我仍然可以在插件中使用普通的wp\\uAjax挂钩!)。但这安全吗?可能出现什么问题?由