如果我想总是更改页面顶部的管理工具栏,那很容易。
add_action(\'admin_bar_menu\', \'add_toolbar_items\', 100);
function add_toolbar_items($admin_bar){
$admin_bar->add_menu( array(
\'id\' => \'my-item\',
\'title\' => \'My Item\',
\'href\' => \'#\',
\'meta\' => array(
\'title\' => __(\'My Item\'),
),
));
}
没问题。我想在页面顶部添加一些东西,但只有当我们在我的插件页面上时。
所以我想这样做:
add_action(\'admin_menu\', \'my_plugin_menu\');
function my_plugin_menu(){
//We add the hook for our menu item on the main menu
$main = add_menu_page( \'My Swell Plugin\', \'My Swell Plugin\', \'edit_posts\', \'blah\', \'generate_main_page\',,__FILE__));
}
function generate_main_page(){
//We are definitely in our plugins page now - let\'s modify the admin toolbar.
add_action(\'admin_bar_menu\', \'add_toolbar_items\', 100);
require_once(\'mainwindow.php\');
}
最合适的回答,由SO网友:Eugene Manuilov 整理而成
在添加新菜单项之前,您需要记住页面的屏幕id并检查当前屏幕id:
add_action( \'admin_menu\', \'my_plugin_menu\' );
function my_plugin_menu() {
global $my_swell_plugin_page;
$my_swell_plugin_page = add_menu_page( \'My Swell Plugin\', \'My Swell Plugin\', \'edit_posts\', \'blah\', \'generate_main_page\' );
}
function generate_main_page() {
//We are definitely in our plugins page now - let\'s modify the admin toolbar.
}
add_action( \'admin_bar_menu\', \'add_toolbar_items\', 100 );
function add_toolbar_items( $admin_bar ) {
global $my_swell_plugin_page;
if ( $my_swell_plugin_page == get_current_screen()->id ) {
$admin_bar->add_menu( array(
\'id\' => \'my-item\',
\'title\' => \'My Item\',
\'href\' => \'#\',
\'meta\' => array(
\'title\' => __( \'My Item\' ),
),
) );
}
}