0

I need to block any user creation email on the site, and I tried overriding any pluggable function that is responsible for mail sending. The only thing that works is

$this->loader->add_filter( 'send_password_change_email', $users, 'disable_email' );
$this->loader->add_filter( 'send_email_change_email', $users, 'disable_email' );

the disable_email method is defined in the Users class

class Users {
  /**
   * Function that will disable email sending when user is created
   *
   * @return bool False.
   */
  public function disable_email() {
    return false;
  }

  ...
}

the $users is just an instance of Users class, and $this->loader is an instance of Loader class, which holds the filters.

But when I create a WordPress user, I want to block the 'New User Registration' mail that admin and the user will get.

What I've read is that I should be able to override the pluggable functions, but the only way to do that is to define them in the root file of the plugin, outside of any class.

So I tried that

if( ! function_exists( 'wp_new_user_notification' ) ) {
  function wp_new_user_notification( $user_id ) {
    return false
  }
}

but checking my mailcatcher (I'm using VVV), it's not working since the mail is still passing through.

I even tried overriding wp_mail() function the same way, but nothing is happening. The mails are still going through.

I'm out of ideas. I need to block the mails, and I have no ideas how to do it :/

2

send_password_change_email and send_email_change_email are the filters used for sending the user an email, if they change their password or an account has been created for them.

It is not responsible for the "New user registration on your site" mail. The function that does this is wp_new_user_notification(), which is actually one of the pluggable functions.

You could simply overwrite it, but I find doing this very tricky. Instead, remove the action (which is actually wp_send_new_user_notifications, which is nothing more than a wrapper for the function itself). The action gets added like so

add_action( 'register_new_user',      'wp_send_new_user_notifications' );
add_action( 'edit_user_created_user', 'wp_send_new_user_notifications', 10, 2 );

If you "reverse" that code, it should work. Assuming your loader works as the regular methods:

$this->loader->remove_action(
    'register_new_user',
    'wp_send_new_user_notifications'
);
$this->loader->remove_action(
    'edit_user_created_user',
    'wp_send_new_user_notifications',
    10,
    2
);
| improve this answer | |
  • 1
    It works! You're a life savior! I hassled with this for 2 hours yesterday :D – dingo_d May 16 '18 at 6:47
  • 1
    @dingo_d You're very welcome! Sometimes a new set of eyes on a problem is all it needs ;) – kero May 16 '18 at 7:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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