0

I successfully managed to create admin bar item using this:

add_action('admin_bar_menu', 'toolbar_link_to_mypage', 999);
function toolbar_link_to_mypage($wp_admin_bar){
    global $wp_admin_bar;

    $theme_menu = array(
        'id' => 'theme_page',
        'title' => __('test', 'test'),
        'href' => 'http://mysite.com/my-page/',
        'meta' => array('class' => 'test')
    );

    $wp_admin_bar->add_node($theme_menu);
}

But I can't figure out what is the correct way to create an admin panel OPTIONS PAGE (not sub-menu item! Two answers are about sub-menus already) for 'href' => '', parameter. I can't simply use add_menu_page(); because that would attach it to left navigation as well. Any help would be great!

2 Answers 2

0

You have to create a new array for the new item and pass the parent parameter as the id of this already created item. In your case, the args array should be like this:

$args = array(
    'id' => 'my-item',
    'title' => 'My Item',
    'href' => '#',
    'parent' => 'theme_page',
    'meta' => array(
        'title' => __('Click Me'),
    ),
);

This post has a complete explanation of how and where you can add items to the admin bar and the usage of the parent parameter.

4
  • I didn't make myself clear enough. I know how to create sub-pages, too but I'm wondering how to create OPTIONS PAGE for admin bar item :)
    – Paul
    Sep 6, 2012 at 9:29
  • Options page? You mean you want to be able to add new menu items from an options page? Sep 6, 2012 at 9:57
  • I can use add_menu_page(); to register new options page in left sidebar in admin panel. But I can't do that in top admin bar - I can only specify link. I'm looking for a way to create options page for each link in that bar.
    – Paul
    Sep 6, 2012 at 10:54
  • You add the page via the regular way, using add_menu_page, and then follow this: justintadlock.com/archives/2011/06/13/… tutorial to remove that page from the sidebar menu. Then, for the href part of your admin bar item, pass the url for the removed settings page. Sep 6, 2012 at 12:30
0

Here is the code that will work :

 add_action('admin_bar_menu', 'toolbar_link_to_mypage', 999);
function toolbar_link_to_mypage($wp_admin_bar){
    global $wp_admin_bar;

    $wp_admin_bar->add_menu(array(
        'id' => 'theme_page',
        'title' => __('test', 'test'),
        'href' => 'http://mysite.com/my-page/',
        'meta' => array('class' => 'test')
    ));

   $wp_admin_bar->add_menu(array(
        'id' => 'theme_page_child',
        'title' => __('Child test', 'child-test'),
        'href' => 'http://mysite.com/my-page/',
        'meta' => array('class' => 'child-test'),
    'parent' => 'theme_page'
    ));

}

In the first add_menu I have added a parent link In the next add_menu I am using another attribute 'parent' which is referring to the id of 'parent' here the 'parent' is 'theme_page' we created above.

1
  • Yes, but I'm wondering how to create OPTIONS PAGE for admin bar item. I know how to create sub-menu item. Any ideas?
    – Paul
    Sep 6, 2012 at 9:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.