WordPress: Disable New User Registration Notifications (E-Mail)
tweak, new user emails
As of WordPress 4.6 it is possible to hook into the wp_send_new_user_notifications action to disable the new user notifications send to site admins or the new user.
// The Parameter "$behaviour" can be set to:
// "none" (no notifications are send);
// "default" (no changes);
// "admin" (notifications send to admin only);
// "user" (notification send to user only);
// "both" (notifications send to admin + user)
public static function limitNewRegistrationNotifications($behaviour){
// do nothing
if ($behaviour == 'default'){
return;
}
// handle user registrations (self registered users)
remove_action('register_new_user', 'wp_send_new_user_notifications');
// new users added via wp-admin are created using add_user() -> edit_user() chain, NOT register_new_user()
// @see https://developer.wordpress.org/reference/functions/add_user/
remove_action('edit_user_created_user', 'wp_send_new_user_notifications', 10, 2);
// notifications disabled ?
if ($behaviour == 'none'){
return;
}
// add custom callback and override the $notify setting with custom behaviour
add_action('register_new_user', function($user_id) use ($behaviour){
// trigger notification
wp_new_user_notification($user_id, null, $behaviour);
});
add_action('edit_user_created_user', function($user_id) use ($behaviour){
// trigger notification
wp_new_user_notification($user_id, null, $behaviour);
});
}
This Tweak is available as part of the Tweakr WordPress Plugin.