函数文档

wp_password_change_notification()

💡 云策文档标注

概述

wp_password_change_notification() 是一个 WordPress 核心函数,用于在用户重置密码时向站点管理员发送电子邮件通知。该函数是可插拔的,允许插件覆盖其行为。

关键要点

  • 函数在用户重置丢失密码时触发,而非在个人资料页面更改密码时。
  • 通过 wp_password_change_notification_email 过滤器可自定义通知邮件内容。
  • 如果管理员邮箱与用户邮箱相同,则跳过通知发送。
  • 函数支持本地化切换,确保邮件使用管理员或站点的语言环境。

代码示例

function wp_password_change_notification( $user ) {
    if ( 0 !== strcasecmp( $user->user_email, get_option( 'admin_email' ) ) ) {
        $admin_user = get_user_by( 'email', get_option( 'admin_email' ) );
        if ( $admin_user ) {
            $switched_locale = switch_to_user_locale( $admin_user->ID );
        } else {
            $switched_locale = switch_to_locale( get_locale() );
        }
        $message = sprintf( __( 'Password changed for user: %s' ), $user->user_login ) . "rn";
        $blogname = wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES );
        $wp_password_change_notification_email = array(
            'to'      => get_option( 'admin_email' ),
            'subject' => __( '[%s] Password Changed' ),
            'message' => $message,
            'headers' => '',
        );
        $wp_password_change_notification_email = apply_filters( 'wp_password_change_notification_email', $wp_password_change_notification_email, $user, $blogname );
        wp_mail(
            $wp_password_change_notification_email['to'],
            wp_specialchars_decode( sprintf( $wp_password_change_notification_email['subject'], $blogname ) ),
            $wp_password_change_notification_email['message'],
            $wp_password_change_notification_email['headers']
        );
        if ( $switched_locale ) {
            restore_previous_locale();
        }
    }
}

注意事项

  • 禁用通知的推荐方法是通过 remove_action( 'after_password_reset', 'wp_password_change_notification' ) 移除钩子。
  • 由于 pluggable 函数在主题加载前定义,在主题的 functions.php 中覆盖此函数可能无效,建议使用插件实现。
  • 使用 wp_password_change_notification_email 过滤器时,确保正确处理邮件数组以避免发送失败。

📄 原文内容

Notifies the blog admin of a user changing password, normally via email.

Parameters

$userWP_Userrequired
User object.

More Information

  • This function is normally called when a user resets a lost password, not if the password is changed on their profile page.
  • This function can be replaced via plugins. If plugins do not redefine these functions, then this will be used instead.

Source

function wp_password_change_notification( $user ) {
	/*
	 * Send a copy of password change notification to the admin,
	 * but check to see if it's the admin whose password we're changing, and skip this.
	 */
	if ( 0 !== strcasecmp( $user->user_email, get_option( 'admin_email' ) ) ) {

		$admin_user = get_user_by( 'email', get_option( 'admin_email' ) );

		if ( $admin_user ) {
			$switched_locale = switch_to_user_locale( $admin_user->ID );
		} else {
			$switched_locale = switch_to_locale( get_locale() );
		}

		/* translators: %s: User name. */
		$message = sprintf( __( 'Password changed for user: %s' ), $user->user_login ) . "rn";
		/*
		 * 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 );

		$wp_password_change_notification_email = array(
			'to'      => get_option( 'admin_email' ),
			/* translators: Password change notification email subject. %s: Site title. */
			'subject' => __( '[%s] Password Changed' ),
			'message' => $message,
			'headers' => '',
		);

		/**
		 * Filters the contents of the password change notification email sent to the site admin.
		 *
		 * @since 4.9.0
		 *
		 * @param array   $wp_password_change_notification_email {
		 *     Used to build wp_mail().
		 *
		 *     @type string $to      The intended recipient - site admin email address.
		 *     @type string $subject The subject of the email.
		 *     @type string $message The body of the email.
		 *     @type string $headers The headers of the email.
		 * }
		 * @param WP_User $user     User object for user whose password was changed.
		 * @param string  $blogname The site title.
		 */
		$wp_password_change_notification_email = apply_filters( 'wp_password_change_notification_email', $wp_password_change_notification_email, $user, $blogname );

		wp_mail(
			$wp_password_change_notification_email['to'],
			wp_specialchars_decode( sprintf( $wp_password_change_notification_email['subject'], $blogname ) ),
			$wp_password_change_notification_email['message'],
			$wp_password_change_notification_email['headers']
		);

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

Hooks

apply_filters( ‘wp_password_change_notification_email’, array $wp_password_change_notification_email, WP_User $user, string $blogname )

Filters the contents of the password change notification email sent to the site admin.

Changelog

Version Description
2.7.0 Introduced.

User Contributed Notes

  1. Skip to note 5 content

    The function is hooked to the 'after_password_reset' action, so I think a simpler way to disable it would be just:

    remove_action( 'after_password_reset', 'wp_password_change_notification' );

  2. Skip to note 6 content

    By default, WordPress sends a notification to the blog admin whenever a user changed the password.
    To disable this just add below code to your theme or plugin.

    if ( ! function_exists( 'wp_password_change_notification' ) ) :
        function wp_password_change_notification( $user ) {
            return;
        }
    endif;

  3. Skip to note 7 content

    Alternative method to prevent these emails being sent, put the following code in your functions.php file of your theme or plugin:

    // disable the email notification to admin when user changes password
    add_filter( 'wp_password_change_notification_email', 'wpdocs_stop_email' );
    function wpdocs_stop_email( $email ) {
    	$email['to'] = ''; //empty the TO: part, will fail to send
    	return $email;
    }

    (I found rsm0128’s suggestion did not work for me in WordPress 6.0)