I would like to turn off the emails that the admin gets when a new user registers. We are getting a lot of emails because of new registrations (they are legitimate registrations) but I just don't want to see the email telling me that someone has signed up again and again.

So far I've tried installing plugins but they don't work. (support even says they don't work any longer). When searching around here the only question I was able to find was Turn off admin emails for new user registrations which is from three years ago and doesn't seem to work either. I tried with this code:

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

    }
}

to my theme functions.php (it has the line @wp_mail commented out) and I've even tried to comment out that line in wp-includes/pluggable.php but I still am getting the emails.

I'm using WordPress 4.5.3 right now. (UPDATED to 4.6 as part of this process)

I'm trying to be a clear as a can. I don't want the email that goes to the user to stop as they still should get that, but how can I make the email to admin stop?

有帮助吗?

解决方案

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

Pluggable functions are one of the more depressing relics of WordPress's past and come with a slew of intricacies. That directly modifying the core file (which is entirely inadvisable, as @Jarmerson mentioned in the comments) did not work makes me suspect that another plugin in your installation may be overwriting the pluggable.

The wp-includes/pluggable.php file is loaded after active plugins and mu-plugins, but before the active theme; this means that the "Pluggable Functions" can only be superseded by declarations in a plugin.

The modification you discovered in the other answer applies to a much older version of WordPress. In the process of replacing any pluggable function, you should start with the original function as it exists in your installation's version (in your case, v4.5.3). In doing so, the solution becomes the following (comments omitted; no lines added, only removed):

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

I've omitted the traditional if( !function_exists() ) check that typically encapsulates a pluggable override because in this instance a potential duplicate declaration error is desirable - it would indicate that another plugin has overwritten the wp_new_user_notification() function before you, and thus that your attempt to do so is being completely ignored.

I'd recommend placing this function in a mu-plugin as it lessens the chance that another plugin should beat you to the punch. In any scenario, do not modify the core file wp-includes/pluggable.php with the above.

其他提示

Approach for WordPress 4.6+

Check out the patch in ticket #36009 that was merged into WordPress version 4.6.

It adds the 'user' option for the $notify input parameter of wp_new_user_notification(), to skip sending those emails to the admin.

How it works

The register_new_user() function contains this part:

do_action( 'register_new_user', $user_id );

The email notifications are activated with:

add_action( 'register_new_user', 'wp_send_new_user_notifications' );

where the callback is defined as:

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

Workaround

We could therefore try this approach (untested) with a custom callback and remove the default one:

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

where we change the default from 'both' to 'user'.

It's worth mentioning that wp_send_new_user_notifications() is also hooked into these actions:

  • network_site_new_created_user
  • network_site_users_created_user
  • network_user_new_created_user
  • edit_user_created_user

We could deal with them in a similar way as described above.

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

Per your original question, I can offer some advice on how to handle unwanted notification email from WordPress user registration and password reset email.

Assuming you have a cPanel environment running your installation, simply follow these steps and you will have effectively removed those particular email.

Access Account Level Filtering from the cPanel admin. In this area you can manage filters for your main account. You want to Create Filter and proceed to create a New Filter for All Mail on Your Account.

In the Rule field select To and equals. Note: You can also create filters for other conditions. The last field is a text area which you input the email address you're working with. Below the rules area is where the magic happens. Select Discard Message. There are also other options.

I've always created an email address I know wont be used and created the filter using it.

I could go on about WordPress pluggable hooks and such, but there is nothing that will do exactly as you would like. Bummer...

许可以下: CC-BY-SA归因
scroll top