BuddyDev

Search

[Resolved] How to disable new user welcome email for admins

  • Participant
    Level: Guru
    Posts: 900
    Tosin on #50853

    Hello Brajesh,

    My site has been having lots of user registrations lately and my inbox notification is now becoming a disturbance, how can I disable the new user registration email for only administrators. I have tried the code below but emails are still being sent.

     // Disable new user registration email in WordPress for admin users only
    function disable_wp_new_user_notification_email($user_id) {
        $user = get_userdata($user_id);
        $user_roles = $user->roles;
    
        // Check if the user has the 'administrator' role
        if (in_array('administrator', $user_roles)) {
            return false; // Disable email notification for admin users
        }
    
        return true; // Enable email notification for non-admin users
    }
    add_filter('wp_new_user_notification_email', 'disable_wp_new_user_notification_email');
    
    // Disable new user registration email in BuddyPress for admin users only
    function disable_bp_new_user_notification_email($user_id) {
        $user = get_userdata($user_id);
        $user_roles = $user->roles;
    
        // Check if the user has the 'administrator' role
        if (in_array('administrator', $user_roles)) {
            remove_action('bp_core_activated_user', 'bp_send_activation_key');
            remove_action('bp_core_signup_send_validation_email', 'bp_core_signup_send_validation_email');
        }
    }
    add_action('bp_loaded', 'disable_bp_new_user_notification_email');
    
    // Disable new user registration email in WooCommerce for admin users only
    function disable_wc_new_user_notification_email($user_id) {
        $user = get_userdata($user_id);
        $user_roles = $user->roles;
    
        // Check if the user has the 'administrator' role
        if (in_array('administrator', $user_roles)) {
            if (get_user_meta($user_id, '_wc_new_customer_data', true)) {
                delete_user_meta($user_id, '_wc_new_customer_data');
            }
        }
    }
    add_action('woocommerce_created_customer', 'disable_wc_new_user_notification_email'); 
  • Participant
    Level: Guru
    Posts: 900
    Tosin on #50854

    This code seemed to work

     // Disable new user registration email in WordPress for admin users only
    function disable_send_new_user_notifications($user_id, $notify = 'user') {
        // Check if user role is administrator
        $user_role = get_user_meta($user_id, 'wp_capabilities', true);
        if (isset($user_role['administrator'])) {
            // User is an administrator, skip sending notification
            return;
        }
    
        if (empty($notify) || 'admin' === $notify) {
            return;
        } elseif ('both' === $notify) {
            // Send new users the email but not the admin.
            $notify = 'user';
        }
        wp_send_new_user_notifications($user_id, $notify);
    }
    
    // Disable default email notifications
    add_action('init', function () {
        remove_action('register_new_user', 'wp_send_new_user_notifications');
        remove_action('edit_user_created_user', 'wp_send_new_user_notifications');
    
        // For BuddyPress
        remove_action('bp_core_activated_user', 'wp_send_new_user_notifications');
    
        // For WooCommerce
        remove_action('woocommerce_created_customer', 'wp_send_new_user_notifications');
    });
    
    // Replace default notifications with custom function
    add_action('register_new_user', 'disable_send_new_user_notifications');
    add_action('edit_user_created_user', 'disable_send_new_user_notifications', 10, 2);
    
    // For BuddyPress
    add_action('bp_core_activated_user', 'disable_send_new_user_notifications');
    
    // For WooCommerce
    add_action('woocommerce_created_customer', 'disable_send_new_user_notifications'); 
  • Participant
    Level: Guru
    Posts: 900
    Tosin on #50861

    After all the stress there is something so simple

     add_filter('wp_new_user_notification_email', '__return_false');
     
  • Keymaster
    (BuddyDev Team)
    Posts: 24623
    Brajesh Singh on #50870

    Hi Tosin,
    Thank you for the topic/replies.

    This is something you will need to be watchful as some of the plugins do override the ‘wp_new_user_notification_email’. All you need to do is apply your filter at lower priority.

    Regards
    Brajesh

  • Participant
    Level: Guru
    Posts: 900
    Tosin on #50877

    I found another solution below

     function disable_admin_new_user_registration_email( $wp_new_user_notification_email_admin, $user, $blogname )
    {
    	//Stop wp_mail() from working
    	add_filter( 'pre_wp_mail', '__return_false' );
    
    	//Return an unchanged value from this filter
    	return $wp_new_user_notification_email_admin;
    }
    add_filter( 'wp_new_user_notification_email_admin', 'disable_admin_new_user_registration_email', 10, 3 ); 

You must be logged in to reply to this topic.

This topic is: resolved