4

Similar to generating an array of the admin menu/submenu:

global $menu;
foreach ( $menu as $group => $item ) {
    echo '<pre>'; print_r( $item ); echo '</pre>';
}

How can I get an array of all of the available menu items in the toolbar? I tried using the global $wp_admin_bar variable, but that seems to not be the right one.

Toolbar

The reason why I want to generate this array is to use this in a plugin of mine to provide an option to choose which items to hide from the toolbar, allowing users to customize their settings. Right now, I manually created an array called $toolbar to choose the items I want to hide myself:

global $wp_admin_bar;
$toolbar = array(
    'wp-logo', // WordPress logo
    'comments', // Comments
    'new-post', // New > Post
    'search' // Search
    );
foreach ( $toolbar as $item ) {
    $wp_admin_bar -> remove_menu( $item );
}

Update [2016-09-16]

I was able to get the entire list by using the following in my plugin settings page:

global $wp_admin_bar;
echo '<pre>'; print_r( $wp_admin_bar ); echo '</pre>';

It's too long to paste in here, so you can view the entire list on this Pastebin link. However, below is a snippet of how it starts. How can I get all of the values that are in [id] => and/or [parent] =>? It looks like I would need to go through a few levels of foreach?

WP_Admin_Bar Object (
    [nodes:WP_Admin_Bar:private] => Array (
            [user-actions] => stdClass Object (
                    [id] => user-actions
                    [title] => 
                    [parent] => my-account
                    [href] => 
                    [meta] => Array (
                            [class] => ab-submenu
                        )

                    [children] => Array (
                            [0] => stdClass Object (
                                    [id] => user-info
                                    [title] => Name
                                    [parent] => user-actions
                                    [href] => http://example.com/wp-admin/profile.php
                                    [meta] => Array (
                                            [tabindex] => -1
                                        )

                                    [children] => Array (
                                        )

                                    [type] => item
                                )

                            [1] => stdClass Object (
                                    [id] => edit-profile
                                    [title] => Edit My Profile
                                    [parent] => user-actions
                                    [href] => http://example.com/wp-admin/profile.php
                                    [meta] => Array (
                                        )

                                    [children] => Array (
                                        )

                                    [type] => item
                                )
and so on...
2
  • it's the good object but you may read it before it is filled. try the action admin_bar_menu with a very low priority to be sure that every actions have been called
    – mmm
    Commented Sep 5, 2016 at 21:51
  • @mmm May you clarify your last comment? From looking into the admin_bar_menu is just the hook. I'm trying to generate a list of all available toolbar menu items in my plugin settings so that that the user can check which ones to hide. Commented Sep 5, 2016 at 22:06

2 Answers 2

2

There is an action hook admin_bar_menu which will provide you an array of admin bar menu items.


add_action('admin_bar_menu', 'get_admin_bar_header_array');
public function get_admin_bar_header_array($admin_bar){
print_r($admin_bar);
}

:)

2
  • to be sure to have the complete bar, set the lowerest priority : add_action('admin_bar_menu', 'get_admin_bar_header_array', PHP_INT_MAX);
    – mmm
    Commented Sep 6, 2016 at 6:19
  • @mmm Maybe I am doing this incorrectly, but I am using this function in my plugin settings so I can get a list of all the toolbar items, similar to how I got a list of the sidebar menu. When I use your function, nothing prints for me. What am I doing wrong exactly? Commented Sep 16, 2016 at 23:29
2

Hook into the action admin_bar_menu, and fetch the menu items with the method get_nodes(), because the item list is private and cannot be accessed directly:

add_action( 'admin_bar_menu', function( \WP_Admin_Bar $wp_admin_bar ) {

    $items = $wp_admin_bar->get_nodes();

    if ( ! $items )
        return;

    print '<pre>';

    foreach ( $items as $id => $item )
    {
        print "$id: " . print_r( $item, TRUE ) . "\n";
    }

    print '</pre>';
}, PHP_INT_MAX );
6
  • I see. However, when I add this function to my settings page, I still don't see anything show up. Should I be calling this in a particular way? Also is the `` in the function intentional? Commented Sep 11, 2016 at 16:44
  • @EthanJinksO'Sullivan I get the admin bar content dumped. The leading \ marks the dependency as one in the global namespace, because WordPress doesn't use an own namespace, while your code probably does (it should!). I've raised the priority, so you get all items now.
    – fuxia
    Commented Sep 11, 2016 at 16:48
  • Thanks for the update. I'm just getting around to your answer now that I have free time to re-approach this so I can put some closure to this question. However, I'm still not seeing the items listed. Does it matter that I am running this function in my plugin's settings page? Commented Sep 16, 2016 at 23:46
  • Well, you have to register the callback before the hook is called. Inside of the settings page is way too late.
    – fuxia
    Commented Sep 16, 2016 at 23:48
  • I see. Thanks for clarifying that for me, I was able to get the array list of the toolbar which I show in my updated question. Now it's a matter of only showing the value based on the [id] or [parent]. If it's easier to chat on The Loop, I'll be online. Commented Sep 17, 2016 at 0:21

Your Answer

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

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