update_right_now_message()
云策文档标注
概述
update_right_now_message() 函数用于在 WordPress 仪表盘的 'At a Glance' 小工具中显示 WordPress 版本和当前活动主题信息。该函数根据用户权限动态生成内容,并支持通过过滤器进行文本定制。
关键要点
- 函数核心功能是输出 WordPress 版本和活动主题的格式化字符串。
- 根据 current_user_can('switch_themes') 权限决定主题名称的显示方式。
- 如果用户有 update_core 权限且存在核心更新,会添加更新链接。
- 提供 apply_filters('update_right_now_text', $content) 钩子,允许开发者过滤显示的文本。
- 函数自 WordPress 2.5.0 版本引入,最初用于 'Right Now' 小工具。
代码示例
function update_right_now_message() {
$theme_name = wp_get_theme();
if ( current_user_can( 'switch_themes' ) ) {
$theme_name = sprintf( '%1$s', $theme_name );
}
$msg = '';
if ( current_user_can( 'update_core' ) ) {
$cur = get_preferred_from_update_core();
if ( isset( $cur->response ) && 'upgrade' === $cur->response ) {
$msg .= sprintf(
'%s ',
network_admin_url( 'update-core.php' ),
sprintf( __( 'Update to %s' ), $cur->current ? $cur->current : __( 'Latest' ) )
);
}
}
$content = __( 'WordPress %1$s running %2$s theme.' );
$content = apply_filters( 'update_right_now_text', $content );
$msg .= sprintf( '' . $content . '', get_bloginfo( 'version', 'display' ), $theme_name );
echo "$msg";
}注意事项
- 函数依赖于多个核心函数,如 wp_get_theme()、current_user_can() 和 get_bloginfo(),确保这些函数正常工作。
- 文本翻译使用 __() 函数,支持国际化。
- 在 3.8.0 版本前,小工具名为 'Right Now',更新后改为 'At a Glance',但函数名保持不变。
原文内容
Displays WordPress version and active theme in the ‘At a Glance’ dashboard widget.
Source
function update_right_now_message() {
$theme_name = wp_get_theme();
if ( current_user_can( 'switch_themes' ) ) {
$theme_name = sprintf( '<a href="themes.php">%1$s</a>', $theme_name );
}
$msg = '';
if ( current_user_can( 'update_core' ) ) {
$cur = get_preferred_from_update_core();
if ( isset( $cur->response ) && 'upgrade' === $cur->response ) {
$msg .= sprintf(
'<a href="%s" class="button" aria-describedby="wp-version">%s</a> ',
network_admin_url( 'update-core.php' ),
/* translators: %s: WordPress version number, or 'Latest' string. */
sprintf( __( 'Update to %s' ), $cur->current ? $cur->current : __( 'Latest' ) )
);
}
}
/* translators: 1: Version number, 2: Theme name. */
$content = __( 'WordPress %1$s running %2$s theme.' );
/**
* Filters the text displayed in the 'At a Glance' dashboard widget.
*
* Prior to 3.8.0, the widget was named 'Right Now'.
*
* @since 4.4.0
*
* @param string $content Default text.
*/
$content = apply_filters( 'update_right_now_text', $content );
$msg .= sprintf( '<span id="wp-version">' . $content . '</span>', get_bloginfo( 'version', 'display' ), $theme_name );
echo "<p id='wp-version-message'>$msg</p>";
}
Hooks
- apply_filters( ‘update_right_now_text’, string $content )
-
Filters the text displayed in the ‘At a Glance’ dashboard widget.
Changelog
| Version | Description |
|---|---|
| 2.5.0 | Introduced. |