13

So, WordPress 4.3 has a new password system as we all know. Unfortunately, this new system has done away with the ability to NOT send new users an email.

My client was using a system where he sent a custom email to his clients, manually registering their emails, and then sending them an email with the login info with a custom message. We are aware that this new system is trying to be more secure, but this isn't working for the amount of control he would like.

I've found the following code in my search for a solution to turn these emails off, but I think they only turn off the notification emails for if a user's email is CHANGED for previously registered users, not when it's first created:

add_filter( 'send_password_change_email', '__return_false');
add_filter( 'send_email_change_email', '__return_false');

Does anyone know of any way to turn off these initial password emails sent after registration?

Thank you.

1
  • 2
    Have you tried this? add_filter( 'user_registration_email', '__return_false');
    – Abhik
    Commented Aug 23, 2015 at 5:06

5 Answers 5

10

You can intercept this email before it is sent using the phpmailer_init hook.

By default, this hook fires before any email is sent. In the function below, $phpmailer will be an instance of PHPMailer, and you can use its methods to remove the default recipient and manipulate the email before it is sent.

add_action('phpmailer_init', 'wse199274_intercept_registration_email');
function wse199274_intercept_registration_email($phpmailer){
    $admin_email = get_option( 'admin_email' );

    # Intercept username and password email by checking subject line
    if( strpos($phpmailer->Subject, 'Your username and password info') ){
        # clear the recipient list
        $phpmailer->ClearAllRecipients();
        # optionally, send the email to the WordPress admin email
        $phpmailer->AddAddress($admin_email);
    }else{
        #not intercepted
    }
}
1
  • this works flawlessly, completely solved my issue and is perfect for other uses as well. Thanks for a great answer!
    – pattyd
    Commented Feb 19, 2017 at 20:33
3

Actually it depends how you create the new user. If you do it from administration - Users - Add New you are right. In 4.3 unfortunatelly you cannot disable sending the notification email. But if you really want to create a new user without the email, there is a way.

You can create a small plugin where you'd create a new account by yourself via wp_insert_user function, which doesn't send any email by default.

This function can be called like this.

wp_insert_user( $userdata );

Where the userdata parameter is an array where you can pass all needed information.

$userdata = array(
    'user_login'  =>  'login',
    'user_pass'   =>  'password',
);

$user_id = wp_insert_user( $userdata ) ;

//On success
if ( ! is_wp_error( $user_id ) ) {
    echo "User created : ". $user_id;
}

For more informations check codex here.

1
  • So this function is called whenever a new user is created I'm assuming?
    – Lisa
    Commented Aug 23, 2015 at 4:55
3

The wp_new_user_notification function is pluggable, so you can override it by defining your own. You should be able to copy the entire function from wp-includes/pluggable.php into your plugin (or functions.php) and remove the line that sends out the email.

1
  • 1
    You can't override wp_new_user_notification in theme's functions.php, because pluggable functions are loaded before theme. It must be overriden in plugin or mu-plugin. Read more: wordpress.stackexchange.com/a/55966/56759
    – fandasson
    Commented Oct 16, 2015 at 13:16
1

To solve this in your theme create new mu-plugin by creating new file wp_new_user_notifications.php (name is up to you) and put it in wp-content/mu-plugins folder. If there is no folder like that, create one. Remember to put the php file directly into the folder, not to any subfolder.

Than continue as Simon suggested - copy wp_new_user_notifications.php method from wp-includes/pluggable.php into your brand new wp_new_user_notifications.php file and modify as needed.

To answer your question: for turning off initial password e-mails is enough to remove last wp_mail method call.

0
add_filter('send_password_change_email', '__return_false');

Works. But its essential that its added to the plugin it self and not the functions.php for the theme.

Like.

class ... {
  public function __construct() {
    ...
    $this->init_hooks();
  }

  public function init_hooks() {
    add_filter('send_password_change_email', '__return_false');
  }
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.