wp_admin_bar_edit_site_menu()
云策文档标注
概述
wp_admin_bar_edit_site_menu() 函数用于在 WordPress 工具栏中添加“编辑站点”链接,仅当激活块主题且用户具有相应权限时显示。
关键要点
- 函数接受一个 WP_Admin_Bar 实例作为必需参数。
- 仅在块主题激活时显示链接,否则函数提前返回。
- 要求用户具有 edit_theme_options 权限且不在管理后台页面。
- 使用 WP_Admin_Bar::add_node() 添加节点,链接指向站点编辑器,包含 postType、postId 和 canvas 查询参数。
- 相关函数包括 wp_is_block_theme()、current_user_can()、is_admin() 等。
代码示例
function wp_admin_bar_edit_site_menu( $wp_admin_bar ) {
global $_wp_current_template_id;
// Don't show if a block theme is not activated.
if ( ! wp_is_block_theme() ) {
return;
}
// Don't show for users who can't edit theme options or when in the admin.
if ( ! current_user_can( 'edit_theme_options' ) || is_admin() ) {
return;
}
$wp_admin_bar->add_node(
array(
'id' => 'site-editor',
'title' => __( 'Edit Site' ),
'href' => add_query_arg(
array(
'postType' => 'wp_template',
'postId' => $_wp_current_template_id,
'canvas' => 'edit',
),
admin_url( 'site-editor.php' )
),
)
);
}注意事项
- 从 WordPress 5.9.0 版本引入,6.3.0 版本添加了 $_wp_current_template_id 全局变量,6.6.0 版本添加了 canvas 查询参数。
- 链接仅在特定条件下显示,开发者需确保主题和用户权限设置正确。
原文内容
Adds the “Edit Site” link to the Toolbar.
Parameters
$wp_admin_barWP_Admin_Barrequired-
The WP_Admin_Bar instance.
Source
function wp_admin_bar_edit_site_menu( $wp_admin_bar ) {
global $_wp_current_template_id;
// Don't show if a block theme is not activated.
if ( ! wp_is_block_theme() ) {
return;
}
// Don't show for users who can't edit theme options or when in the admin.
if ( ! current_user_can( 'edit_theme_options' ) || is_admin() ) {
return;
}
$wp_admin_bar->add_node(
array(
'id' => 'site-editor',
'title' => __( 'Edit Site' ),
'href' => add_query_arg(
array(
'postType' => 'wp_template',
'postId' => $_wp_current_template_id,
'canvas' => 'edit',
),
admin_url( 'site-editor.php' )
),
)
);
}