WordPress – How to disable new user notification emails

The following code can be used to disable the WordPress new user notification emails.

There are two notification emails –

  1. the user notification – these are the emails the user receives to confirm their email address before they can choose a password
  2. the administrator notification -these are the “New User Registration” emails that the site administrator receives each time a new user registers to the site.

If you’re not sure where to place this code I highly recommend you read How to create a WordPress plugin for your custom functions.

How to disable the administrator notification

This code will just just disable the administrator notification.

// DISABLE ADMIN default WordPress new user notification emails
if ( ! function_exists ( 'wp_new_user_notification' ) ) :
    function wp_new_user_notification( $user_id, $deprecated = null, $notify = '' ) {

        global $wpdb, $wp_hasher;
        $user = get_userdata( $user_id );

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

        // Generate something random for a password reset key.
        $key = wp_generate_password( 20, false );

        /** This action is documented in wp-login.php */
        do_action( 'retrieve_password_key', $user->user_login, $key );

        // Now insert the key, hashed, into the DB.
        if ( empty( $wp_hasher ) ) {
            $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 ) );

        $switched_locale = switch_to_locale( get_user_locale( $user ) );

        $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);
    }
endif;

How to disable BOTH notification emails

This code will disable BOTH notification emails – this is only useful if you’re using a third-party plugin, for example Gravity Forms, to create user accounts, specify passwords and notify the users.

// DISABLE BOTH default WordPress new user notification emails
if ( ! function_exists( 'wp_new_user_notification' ) ) :
    function wp_new_user_notification( $user_id, $deprecated = null, $notify = '' ) {
        return;
    }
endif;

How does it work?

The wp_new_user_notification function is the part of the WordPress core that handles the email notification.

It’s also a pluggable function – meaning we can call it through a theme or a plugin and completely override it.

The code above does this and returns nothing, meaning no email is created or sent when it is triggered by a new user registering.

You may also be interested in ….