update_nag()
云策文档标注
概述
update_nag() 函数用于返回 WordPress 核心更新的通知消息,主要面向管理员或具有更新权限的用户。该函数在非更新页面且检测到可用更新时,会输出一个警告类型的 admin notice。
关键要点
- 函数返回 void 或 false,无返回值时输出通知
- 在 Multisite 环境中,仅允许具有 'update_core' 权限的用户查看通知
- 在 'update-core.php' 页面不显示通知,避免重复
- 通过 get_preferred_from_update_core() 检查是否有可用更新
- 根据用户权限(current_user_can('update_core'))生成不同的消息内容
- 使用 wp_admin_notice() 输出带有 'update-nag' 和 'inline' 类的警告通知
注意事项
在非 Multisite 安装中,可通过权限控制仅向管理员显示更新通知。
原文内容
Returns core update notification message.
Source
function update_nag() {
global $pagenow;
if ( is_multisite() && ! current_user_can( 'update_core' ) ) {
return false;
}
if ( 'update-core.php' === $pagenow ) {
return;
}
$cur = get_preferred_from_update_core();
if ( ! isset( $cur->response ) || 'upgrade' !== $cur->response ) {
return false;
}
$version_url = sprintf(
/* translators: %s: WordPress version. */
esc_url( __( 'https://wordpress.org/documentation/wordpress-version/version-%s/' ) ),
sanitize_title( $cur->current )
);
if ( current_user_can( 'update_core' ) ) {
$msg = sprintf(
/* translators: 1: URL to WordPress release notes, 2: New WordPress version, 3: URL to network admin, 4: Accessibility text. */
__( '<a href="%1$s">WordPress %2$s</a> is available! <a href="%3$s" aria-label="%4$s">Please update now</a>.' ),
$version_url,
$cur->current,
network_admin_url( 'update-core.php' ),
esc_attr__( 'Please update WordPress now' )
);
} else {
$msg = sprintf(
/* translators: 1: URL to WordPress release notes, 2: New WordPress version. */
__( '<a href="%1$s">WordPress %2$s</a> is available! Please notify the site administrator.' ),
$version_url,
$cur->current
);
}
wp_admin_notice(
$msg,
array(
'type' => 'warning',
'additional_classes' => array( 'update-nag', 'inline' ),
'paragraph_wrap' => false,
)
);
}
Changelog
| Version | Description |
|---|---|
| 2.3.0 | Introduced. |
Skip to note 2 content
Josh Brown
Only show the update nag to administrators:
Note: This is for installations that are not multisite.