How to Disable New User Notifications in WordPress

我的 WordPress 網站有安裝「Social Login WordPress Plugin – AccessPress Social Login Lite」外掛,也造成每天都有大約 1~3人的新會員註冊。已經在AccessPress Social Login Lite 裡設定不要收到通知信:「Do not send email notification to both user and site admin.」,但還是一直收到通知信。

然後在WordPress 的設定裡找不到可以關掉通知的地方,所以每天都會收到超多的這種垃圾信:《摩卡平價小舖》通知:新使用者註冊

由於是寄到 Gmail,手機三不五時會彈通知出來,實在很煩。

Max試著安裝外掛:
Better Notifications for WordPress
https://tw.wordpress.org/plugins/bnfw/

有新增 for Admin 和 for User 的,但結果還是都一樣會寄送Email.


改安裝外掛:
Disable New User Notification Emails
https://wordpress.org/plugins/disable-new-user-notifications/

這個外掛,完全沒有地方可以設定,因為:This plugin disables new user notification emails in WordPress. That is all.

「Disable New User Notification Emails」source code: class-am-dashboard-widget-extend-feed.php

<?php
 if ( ! class_exists( 'AM_Dashboard_Widget_Extend_Feed' ) ) {
     /**
      * Awesome Motive Events and News Feed.
      *
      * This appends additional blog feeds to the WordPress Events and News Feed Widget
      * available in the WP-Admin Dashboard.
      *
      * @package    AwesomeMotive
      * @author     AwesomeMotive Team
      * @license    GPL-2.0+
      * @copyright  Copyright (c) 2018, Awesome Motive LLC
      * @version    1.0.2
      */
     class AM_Dashboard_Widget_Extend_Feed {
     /**      * The number of feed items to show.      *      * @since 1.0.0      *      * @var int      */     const FEED_COUNT = 6;     /**      * Construct.      *      * @since 1.0.0      */     public function __construct() {         // Actions.         add_action( 'wp_feed_options', array( $this, 'dashboard_update_feed_urls' ), 10, 2 );         // Filters.         add_filter( 'dashboard_secondary_items', array( $this, 'dashboard_items_count' ) );     }     /**      * Set the number of feed items to show.      *      * @since 1.0.0      *      * @return int Count of feed items.      */     public function dashboard_items_count() {         /**          * Apply the filters am_dashboard_feed_count for letting an admin          * override this count.          */         return (int) apply_filters( 'am_dashboard_feed_count', self::FEED_COUNT );     }     /**      * Update the planet feed to add other AM blog feeds.      *      * @since 1.0.0      *      * @param object $feed SimplePie feed object (passed by reference).      * @param string $url URL of feed to retrieve (original planet feed url). If an array of URLs, the feeds are merged.      */     public function dashboard_update_feed_urls( $feed, $url ) {         global $pagenow;         // Return early if not on the right page.         if ( 'admin-ajax.php' !== $pagenow ) {             return;         }         /**          * Return early if not on the right feed.          * We want to modify the feed URLs only for the          * WordPress Events & News Dashboard Widget          */         if ( strpos( $url, 'planet.wordpress.org' ) === false ) {             return;         }         // Build the feed sources.         $all_feed_urls = $this->get_feed_urls( $url );         // Update the feed sources.         $feed->set_feed_url( $all_feed_urls );     }     /**      * Get the feed URLs for various active AM Products.      *      * @since 1.0.0      *      * @param string $url Planet Feed URL.      *      * @return array Array of Feed URLs.      */     public function get_feed_urls( $url ) {         // Initialize the feeds array.         $feed_urls = array(             'https://www.wpbeginner.com/feed/',             'https://www.isitwp.com/feed/',             $url,         );         // Check if MonsterInsights is active.         if ( function_exists( 'MonsterInsights' ) ) {             $feed_urls[] = 'https://www.monsterinsights.com/feed/';         }         // Check if WPForms is active.         if ( function_exists( 'wpforms' ) ) {             $feed_urls[] = 'https://wpforms.com/feed/';         }         // Check if OptinMonster is active.         if ( class_exists( 'OMAPI', false ) ) {             $feed_urls[] = 'https://optinmonster.com/feed/';         }         // Return the feed URLs.         return array_unique( $feed_urls );     } } // Create an instance. new AM_Dashboard_Widget_Extend_Feed();
 }

source code: cwwp-disable-new-user-emails.php

/* AM Dashboard Widget */
 include('class-am-dashboard-widget-extend-feed.php');
 if ( ! function_exists( 'wp_new_user_notification' ) ) {
     if ( version_compare( $GLOBALS['wp_version'], '4.3', '<' ) ) {
         // This definition is for WP versions before 4.3. 4.3 broke this function badly, and 4.4 did no better at fixing it.
         function wp_new_user_notification( $user_id, $plaintext_pass = '' ) {
             if ( empty( $plaintext_pass ) ) {
                 return;
             }
         $user      = get_userdata( $user_id );         $user_login = stripslashes( $user->user_login );         $user_email = stripslashes( $user->user_email );         // The blogname option is escaped with esc_html on the way into the database in sanitize_option         // we want to reverse this for the plain text arena of emails.         $blogname = wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES );         $message  = sprintf( __( 'Username: %s' ), $user_login ) . "\r\n";         $message .= sprintf( __( 'Password: %s' ), $plaintext_pass ) . "\r\n";         $message .= wp_login_url() . "\r\n";         wp_mail( $user_email, sprintf( __( '[%s] Your username and password' ), $blogname ), $message );     } } else {     // This definition is for WP versions after 4.3.     function wp_new_user_notification( $user_id, $deprecated = null, $notify = '' ) {         if ( $deprecated !== null ) {             _deprecated_argument( __FUNCTION__, '4.3.1' );         }         // `$deprecated was pre-4.3 `$plaintext_pass`. An empty `$plaintext_pass` didn't sent a user notifcation.         if ( 'admin' === $notify || ( empty( $deprecated ) && empty( $notify ) ) ) {             return;         }         global $wpdb, $wp_hasher;         $user = get_userdata( $user_id );         // The blogname option is escaped with esc_html on the way into the database in sanitize_option         // we want to reverse this for the plain text arena of emails.         $blogname = wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES );         // Generate something random for a password reset key.         $key = wp_generate_password( 20, false );         /** This action is documented in wp-login.php */         do_action( 'retrieve_password_key', $user->user_login, $key );         // Now insert the key, hashed, into the DB.         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 );     } }
 }
 require plugin_dir_path( FILE ) . 'class-am-dashboard-widget-extend-feed.php';

經測試在 wordpress 5.3,還是會收到信 Q_Q;

再挑戰看看使用:Tweakr – Utility Toolkit
https://wordpress.org/plugins/tweakr/

經測試在 wordpress 5.3,還是會收到信 Q_Q;

只好直接在Gmail 裡設定「篩選條件」,直接在伺服器刪除。


其他解法:透過修改外觀主題的 functions.php:
https://wordpress.stackexchange.com/questions/236122/turn-off-new-user-registration-emails

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

修改 functions.php 的解法 2:
Turn off admin emails for new user registrations
https://wordpress.stackexchange.com/questions/90311/turn-off-admin-emails-for-new-user-registrations

The function that you’re looking for is wp_new_user_notification it’s a pluggable function. Pluggable functions are function that let you override WordPress Core functionalities.

You can override, simply by:

  1. Create a file in mu-plugins folder, you can call whatever you want, but you should give a meaningful name, like custom-new-user-notification.php
  2. Copy the default function and wrapping it in a if statement checking if the function doesn’t exist. (it’s a best practice if not it can produce errors)
  3. Change the message and subject ( or whatever you want to change)

Example:

   <?php
        /*
        Plugin Name:     Custom User Notification
        Plugin URI:      http://www.example.com
        Description:     Pligin description
        Version:         1.0
        Author:          Your Name
        Author URI:      http://www.authorurl.com
        */


        if( !function_exists( 'wp_new_user_notification' ) ){

            function wp_new_user_notification( $user_id, $deprecated = null, $notify = '' ) {
                if ( $deprecated !== null ) {
                    _deprecated_argument( __FUNCTION__, '4.3.1' );
                }

                global $wpdb, $wp_hasher;
                $user = get_userdata( $user_id );

                // The blogname option is escaped with esc_html on the way into the database in sanitize_option
                // we want to reverse this for the plain text arena of emails.
                $blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);

                if ( 'user' !== $notify ) {
                    $switched_locale = switch_to_locale( get_locale() );
                    $message  = sprintf( __( 'New user registration on your site %s:' ), $blogname ) . "\r\n\r\n";
                    $message .= sprintf( __( 'Username: %s' ), $user->user_login ) . "\r\n\r\n";
                    $message .= sprintf( __( 'Email: %s' ), $user->user_email ) . "\r\n";

                    @wp_mail( get_option( 'admin_email' ), sprintf( __( '[%s] New User Registration' ), $blogname ), $message );

                    if ( $switched_locale ) {
                        restore_previous_locale();
                    }
                }

                // `$deprecated was pre-4.3 `$plaintext_pass`. An empty `$plaintext_pass` didn't sent a user notification.
                if ( 'admin' === $notify || ( empty( $deprecated ) && empty( $notify ) ) ) {
                    return;
                }

                // Generate something random for a password reset key.
                $key = wp_generate_password( 20, false );

                /** This action is documented in wp-login.php */
                do_action( 'retrieve_password_key', $user->user_login, $key );

                // Now insert the key, hashed, into the DB.
                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 ) );

                $switched_locale = switch_to_locale( get_user_locale( $user ) );

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

                if ( $switched_locale ) {
                    restore_previous_locale();
                }
            }

        }

再進階一點的問題,就是「如何客製化歡迎使用者加入的確認信」,客製化Welcome email 的 plugin 還滿多的。


服用下面的code 來停用寄發通知:

Add the following code to the function.php file of your active theme

// Fix New User Email Notification
if( !function_exists ( 'wp_new_user_notification')){
 function wp_new_user_notification ( $user_id, $notify = '' ) { }
}

進階功能:「客製化歡迎使用者加入的確認信」

However if the above code is used then all notifications will be disabled including the confirmation of usernames, passwords for registered users. So I recommend you should add the following code to the function.php file:

if ( !function_exists ( 'wp_new_user_notification' ) ) :
 function wp_new_user_notification( $user_id, $notify = '' ) {
 $user = get_userdata( $user_id );
 $user_login = stripslashes ($user-> user_login);
 $user_email = stripslashes ($user-> user_email);
 $blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
 if ( empty($notify) ) {
 return;
 }
 $message = sprintf(('Username: %s'), $user_login) . "\r\n"; $message .= sprintf(( 'Password: %s', $notify) . "\r\n";
 $message .= wp_login_url() . "\r\n";
 wp_mail($user_email, sprintf(__('[%s] Your username and password'), $blogname), $message);
 }

有可能需要重啟你的 apache2 service 才能套用到設定值!

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *