$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Turn off admin emails for new user registrations|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Turn off admin emails for new user registrations

matteradmin10PV0评论

How can I turn off email notifications for the user and admin when a new user is registered?

I've seen a few suggestions and plugins but none appear to work. One was to take the function from one of the plugins:

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;

The questions and suggestions were quite old so maybe WP 3.5 over rides something.

On signup I'm still getting the admin email and an email to the user.

I don't want to block the forgotten password email though.

How can I turn off email notifications for the user and admin when a new user is registered?

I've seen a few suggestions and plugins but none appear to work. One was to take the function from one of the plugins:

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;

The questions and suggestions were quite old so maybe WP 3.5 over rides something.

On signup I'm still getting the admin email and an email to the user.

I don't want to block the forgotten password email though.

Share Improve this question asked Mar 11, 2013 at 10:38 RobRob 1,42613 gold badges41 silver badges68 bronze badges 2
  • Is there an installable plugin available that has the same functionality? – orschiro Commented May 30, 2016 at 5:15
  • 1 @orschiro Here's it - wordpress/plugins/disable-new-user-notifications – Paras Shah Commented Dec 2, 2016 at 6:03
Add a comment  | 

4 Answers 4

Reset to default 9

Function wp_new_user_notification is pluggable. It means that you can override it by declaring your version of this function in your plugin/theme.

So, if you wish to disable all notifications completely, do it like this:

if ( !function_exists( 'wp_new_user_notification' ) ) :
function wp_new_user_notification( $user_id, $plaintext_pass = '' ) {
    return;
}
endif;

However I wouldn't recommend you to disable all notifications, and would recommend you to send notification to an user at least (How does an user find out his password?). So in this case your code should be following:

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;

According to this page it would suffice to use this code (to disable all notifications completely):

if ( !function_exists('wp_new_user_notification') ) {    
  function wp_new_user_notification( ) {}    
}

It's pretty close to the given answer, but a little shorter. I thought of sharing this for what it's worth.

As of 4.6 there is a parameter to disable admin notifications. https://developer.wordpress/reference/functions/wp_new_user_notification/

  • @since 4.6.0 The $notify parameter accepts 'user' for sending notification only to the user created.
  • @param string $notify Optional. Type of notification that should happen. Accepts 'admin' or an empty string (admin only), 'user', or 'both' (admin and user). Default empty. */
  • function wp_new_user_notification( $user_id, $deprecated = null, $notify = 'user' )

Had to do this today, and found many of these solutions seem pretty outdated. This looks to be a better way without overwriting pluggable functions. This isn't my exact code but should be a good reference.

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

  }
} 

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far