wp_admin_bar_updates_menu()
云策文档标注
概述
wp_admin_bar_updates_menu() 函数用于在 WordPress 管理工具栏中添加更新链接,当有主题、插件或核心更新可用时显示。它基于 wp_get_update_data() 获取更新数据,并动态生成更新数量和文本。
关键要点
- 函数接受一个必需的参数 $wp_admin_bar,类型为 WP_Admin_Bar 实例。
- 使用 wp_get_update_data() 检查更新总数,若无更新则直接返回。
- 通过 _n() 和 number_format_i18n() 本地化更新文本和数字格式。
- 调用 WP_Admin_Bar::add_node() 添加节点,链接到 network_admin_url('update-core.php')。
- 自 WordPress 3.1.0 版本引入。
代码示例
function wp_admin_bar_updates_menu( $wp_admin_bar ) {
$update_data = wp_get_update_data();
if ( ! $update_data['counts']['total'] ) {
return;
}
$updates_text = sprintf(
/* translators: Hidden accessibility text. %s: Total number of updates available. */
_n( '%s update available', '%s updates available', $update_data['counts']['total'] ),
number_format_i18n( $update_data['counts']['total'] )
);
$icon = '';
$title = '' . number_format_i18n( $update_data['counts']['total'] ) . '';
$title .= '' . $updates_text . '';
$wp_admin_bar->add_node(
array(
'id' => 'updates',
'title' => $icon . $title,
'href' => network_admin_url( 'update-core.php' ),
)
);
}
原文内容
Provides an update link if theme/plugin/core updates are available.
Parameters
$wp_admin_barWP_Admin_Barrequired-
The WP_Admin_Bar instance.
Source
function wp_admin_bar_updates_menu( $wp_admin_bar ) {
$update_data = wp_get_update_data();
if ( ! $update_data['counts']['total'] ) {
return;
}
$updates_text = sprintf(
/* translators: Hidden accessibility text. %s: Total number of updates available. */
_n( '%s update available', '%s updates available', $update_data['counts']['total'] ),
number_format_i18n( $update_data['counts']['total'] )
);
$icon = '<span class="ab-icon" aria-hidden="true"></span>';
$title = '<span class="ab-label" aria-hidden="true">' . number_format_i18n( $update_data['counts']['total'] ) . '</span>';
$title .= '<span class="screen-reader-text updates-available-text">' . $updates_text . '</span>';
$wp_admin_bar->add_node(
array(
'id' => 'updates',
'title' => $icon . $title,
'href' => network_admin_url( 'update-core.php' ),
)
);
}
Changelog
| Version | Description |
|---|---|
| 3.1.0 | Introduced. |