default_password_nag()
云策文档标注
概述
default_password_nag() 是一个 WordPress 函数,用于在管理员界面显示一个通知,提醒用户更改自动生成的密码。它检查用户是否设置了默认密码提醒,并在特定条件下输出一个包含链接的 admin notice。
关键要点
- 函数检查全局变量 $pagenow 是否为 'profile.php' 或用户选项 'default_password_nag' 是否存在,以决定是否显示通知。
- 使用 wp_admin_notice() 输出一个带有自定义消息和样式的 admin notice,消息包含指向个人资料页面的链接和关闭提醒的选项。
- 依赖于多个辅助函数,如 get_user_option()、__()、esc_url() 和 get_edit_profile_url(),用于获取用户数据、翻译文本和生成安全 URL。
代码示例
function default_password_nag() {
global $pagenow;
// Short-circuit it.
if ( 'profile.php' === $pagenow || ! get_user_option( 'default_password_nag' ) ) {
return;
}
$default_password_nag_message = sprintf(
'%1$s %2$s',
__( 'Notice:' ),
__( 'You are using the auto-generated password for your account. Would you like to change it?' )
);
$default_password_nag_message .= sprintf(
'%2$s | ',
esc_url( get_edit_profile_url() . '#password' ),
__( 'Yes, take me to my profile page' )
);
$default_password_nag_message .= sprintf(
'%2$s',
'?default_password_nag=0',
__( 'No thanks, do not remind me again' )
);
wp_admin_notice(
$default_password_nag_message,
array(
'additional_classes' => array( 'error', 'default-password-nag' ),
'paragraph_wrap' => false,
)
);
}注意事项
- 此函数仅在管理员界面中运行,依赖于 WordPress 核心环境,不应在主题或插件中直接调用,除非需要自定义密码提醒逻辑。
- 消息使用 __() 进行国际化,确保在多语言站点中正确显示翻译文本。
- 从 WordPress 2.8.0 版本引入,开发者应检查兼容性,避免在旧版本中使用。
原文内容
Source
function default_password_nag() {
global $pagenow;
// Short-circuit it.
if ( 'profile.php' === $pagenow || ! get_user_option( 'default_password_nag' ) ) {
return;
}
$default_password_nag_message = sprintf(
'<p><strong>%1$s</strong> %2$s</p>',
__( 'Notice:' ),
__( 'You are using the auto-generated password for your account. Would you like to change it?' )
);
$default_password_nag_message .= sprintf(
'<p><a href="%1$s">%2$s</a> | ',
esc_url( get_edit_profile_url() . '#password' ),
__( 'Yes, take me to my profile page' )
);
$default_password_nag_message .= sprintf(
'<a href="%1$s" id="default-password-nag-no">%2$s</a></p>',
'?default_password_nag=0',
__( 'No thanks, do not remind me again' )
);
wp_admin_notice(
$default_password_nag_message,
array(
'additional_classes' => array( 'error', 'default-password-nag' ),
'paragraph_wrap' => false,
)
);
}
Changelog
| Version | Description |
|---|---|
| 2.8.0 | Introduced. |