函数文档

new_user_email_admin_notice()

💡 云策文档标注

概述

new_user_email_admin_notice() 函数用于在用户更改邮箱地址后,在管理后台显示一个通知,提醒用户检查新邮箱以确认更改。

关键要点

  • 函数在用户个人资料页面(profile.php)更新后触发,检查是否存在待确认的新邮箱地址。
  • 使用 get_user_meta() 获取 _new_email 元数据,并显示一个信息类型的 wp_admin_notice() 通知。
  • 通知消息通过 __() 函数进行本地化,并使用 esc_html() 进行转义以确保安全。

代码示例

function new_user_email_admin_notice() {
	global $pagenow;

	if ( 'profile.php' === $pagenow && isset( $_GET['updated'] ) ) {
		$email = get_user_meta( get_current_user_id(), '_new_email', true );
		if ( $email ) {
			$message = sprintf(
				/* translators: %s: New email address. */
				__( 'Your email address has not been updated yet. Please check your inbox at %s for a confirmation email.' ),
				'' . esc_html( $email['newemail'] ) . ''
			);
			wp_admin_notice( $message, array( 'type' => 'info' ) );
		}
	}
}

注意事项

  • 此函数最初在 WordPress 3.0.0 中引入,并在 4.9.0 版本中从 wp-admin/includes/ms.php 移出,不再仅限于多站点环境。
  • 依赖于全局变量 $pagenow 和 $_GET['updated'] 参数来确保仅在特定条件下显示通知。

📄 原文内容

Adds an admin notice alerting the user to check for confirmation request email after email address change.

Source

function new_user_email_admin_notice() {
	global $pagenow;

	if ( 'profile.php' === $pagenow && isset( $_GET['updated'] ) ) {
		$email = get_user_meta( get_current_user_id(), '_new_email', true );
		if ( $email ) {
			$message = sprintf(
				/* translators: %s: New email address. */
				__( 'Your email address has not been updated yet. Please check your inbox at %s for a confirmation email.' ),
				'<code>' . esc_html( $email['newemail'] ) . '</code>'
			);
			wp_admin_notice( $message, array( 'type' => 'info' ) );
		}
	}
}

Changelog

Version Description
4.9.0 This function was moved from wp-admin/includes/ms.php so it’s no longer Multisite specific.
3.0.0 Introduced.