wp_admin_bar_appearance_menu()
云策文档标注
概述
wp_admin_bar_appearance_menu() 函数用于在 WordPress 管理工具栏的“站点名称”菜单下添加外观相关的子菜单项。它根据用户权限和主题支持的功能动态添加节点,如主题、小工具、菜单、背景和页头设置。
关键要点
- 函数参数为 $wp_admin_bar,类型为 WP_Admin_Bar,必需。
- 使用 WP_Admin_Bar::add_group() 在“site-name”下创建“appearance”组。
- 根据 current_user_can('switch_themes') 权限添加“Themes”节点,链接到 themes.php。
- 需要 edit_theme_options 权限才能继续添加其他节点。
- 基于 current_theme_supports() 检查主题功能,添加“Widgets”、“Menus”、“Background”和“Header”节点。
- 节点使用 __() 和 _x() 进行本地化,href 使用 admin_url() 生成。
代码示例
function wp_admin_bar_appearance_menu( $wp_admin_bar ) {
$wp_admin_bar->add_group(
array(
'parent' => 'site-name',
'id' => 'appearance',
)
);
if ( current_user_can( 'switch_themes' ) ) {
$wp_admin_bar->add_node(
array(
'parent' => 'appearance',
'id' => 'themes',
'title' => __( 'Themes' ),
'href' => admin_url( 'themes.php' ),
)
);
}
if ( ! current_user_can( 'edit_theme_options' ) ) {
return;
}
if ( current_theme_supports( 'widgets' ) ) {
$wp_admin_bar->add_node(
array(
'parent' => 'appearance',
'id' => 'widgets',
'title' => __( 'Widgets' ),
'href' => admin_url( 'widgets.php' ),
)
);
}
if ( current_theme_supports( 'menus' ) || current_theme_supports( 'widgets' ) ) {
$wp_admin_bar->add_node(
array(
'parent' => 'appearance',
'id' => 'menus',
'title' => __( 'Menus' ),
'href' => admin_url( 'nav-menus.php' ),
)
);
}
if ( current_theme_supports( 'custom-background' ) ) {
$wp_admin_bar->add_node(
array(
'parent' => 'appearance',
'id' => 'background',
'title' => _x( 'Background', 'custom background' ),
'href' => admin_url( 'themes.php?page=custom-background' ),
'meta' => array(
'class' => 'hide-if-customize',
),
)
);
}
if ( current_theme_supports( 'custom-header' ) ) {
$wp_admin_bar->add_node(
array(
'parent' => 'appearance',
'id' => 'header',
'title' => _x( 'Header', 'custom image header' ),
'href' => admin_url( 'themes.php?page=custom-header' ),
'meta' => array(
'class' => 'hide-if-customize',
),
)
);
}
}注意事项
- 函数自 WordPress 3.1.0 版本引入。
- 节点添加依赖于用户权限和主题功能支持,需确保正确配置。
- 相关函数包括 WP_Admin_Bar::add_group()、WP_Admin_Bar::add_node()、current_user_can()、current_theme_supports()、__()、_x() 和 admin_url()。
- 被 wp_admin_bar_site_menu() 调用,用于构建站点名称菜单。
原文内容
Adds appearance submenu items to the “Site Name” menu.
Parameters
$wp_admin_barWP_Admin_Barrequired-
The WP_Admin_Bar instance.
Source
function wp_admin_bar_appearance_menu( $wp_admin_bar ) {
$wp_admin_bar->add_group(
array(
'parent' => 'site-name',
'id' => 'appearance',
)
);
if ( current_user_can( 'switch_themes' ) ) {
$wp_admin_bar->add_node(
array(
'parent' => 'appearance',
'id' => 'themes',
'title' => __( 'Themes' ),
'href' => admin_url( 'themes.php' ),
)
);
}
if ( ! current_user_can( 'edit_theme_options' ) ) {
return;
}
if ( current_theme_supports( 'widgets' ) ) {
$wp_admin_bar->add_node(
array(
'parent' => 'appearance',
'id' => 'widgets',
'title' => __( 'Widgets' ),
'href' => admin_url( 'widgets.php' ),
)
);
}
if ( current_theme_supports( 'menus' ) || current_theme_supports( 'widgets' ) ) {
$wp_admin_bar->add_node(
array(
'parent' => 'appearance',
'id' => 'menus',
'title' => __( 'Menus' ),
'href' => admin_url( 'nav-menus.php' ),
)
);
}
if ( current_theme_supports( 'custom-background' ) ) {
$wp_admin_bar->add_node(
array(
'parent' => 'appearance',
'id' => 'background',
'title' => _x( 'Background', 'custom background' ),
'href' => admin_url( 'themes.php?page=custom-background' ),
'meta' => array(
'class' => 'hide-if-customize',
),
)
);
}
if ( current_theme_supports( 'custom-header' ) ) {
$wp_admin_bar->add_node(
array(
'parent' => 'appearance',
'id' => 'header',
'title' => _x( 'Header', 'custom image header' ),
'href' => admin_url( 'themes.php?page=custom-header' ),
'meta' => array(
'class' => 'hide-if-customize',
),
)
);
}
}
Changelog
| Version | Description |
|---|---|
| 3.1.0 | Introduced. |