WordPress Toolbar a.k.a. Admin Bar
First things first. Why WordPress Toolbar a.k.a. Admin Bar? Well, a lot of people knows Toolbar as Admin Bar. Still. To be honest, it was Admin Bar. It was Admin Bar in WordPress 3.2 (released July 4, 2011) and previous. Even more, for backward compatibility, WordPress class for Toolbar is named WP_Admin_Bar. However, since WordPress 3.3 (released December 12, 2011) it is Toolbar and please remember it. To be honest, I have to remember it, too.
All right, to the point. WordPress Toolbar. Nice feature. Very useful. And great. And even more – it is customizable! And we will learn a few basic tricks, how to deal with it. It isn’t a hard thing to do.
Generally, we will discuss three types:
Of course there is more, like removing existing items. I hope you will find it interesting. Maybe I will write about it later, however I can’t promise you that.
All examples uses classes. You may use this code in your plugin or in your theme.
Adding link
First, we need to hook in the right place. Of course we will use add_action
to do that. So, we’ve got something like this:
1 2 3 4 5 6 7 8 9 10 11 12 |
class Toolbar_Test { function admin_bar_menu( $wp_admin_bar ) { } function __construct() { add_action( 'admin_bar_menu', [ $this, 'admin_bar_menu' ] ); } } $toolbar_test = new Toolbar_Test(); |
Please note $wp_admin_bar
variable. This is WordPress instance of WP_Admin_Bar class. Ok, what’s next? Well, we need to add our new node using some arguments.
First, create node (it is simple array, nothing more complicated!):
1 2 3 4 5 6 7 8 9 |
$node = [ 'id' => 'toolbar_test_node', 'title' => 'Lukasz Nowicki', 'href' => 'http://lukasznowicki.info/', 'meta' => [ 'class' => 'toolbar_test_ln', 'title' => 'Lukasz Nowicki - WordPress blog', ], ]; |
And now, just add this node to passed WP_Admin_Bar instance:
1 |
$wp_admin_bar->add_node( $node ); |
Whole code should looks like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
class Toolbar_Test { function admin_bar_menu( $wp_admin_bar ) { $node = [ 'id' => 'toolbar_test_node', 'title' => 'Lukasz Nowicki', 'href' => 'http://lukasznowicki.info/', 'meta' => [ 'class' => 'toolbar_test_ln', 'title' => 'Lukasz Nowicki - WordPress blog', ], ]; $wp_admin_bar->add_node( $node ); } function __construct() { add_action( 'admin_bar_menu', [ $this, 'admin_bar_menu' ] ); } } $toolbar_test = new Toolbar_Test(); |
Write it in functions.php
or create sample plugin and voila! You’ve got new link in your Toolbar! It would be probably first item, because we call admin_bar_menu
pretty early. You may wrote something like this:
1 |
add_action( 'admin_bar_menu', [ $this, 'admin_bar_menu' ], 9999 ); |
and probably our first item immediately becomes last item of Toolbar.
Adding groups
No surprise, this is very similar to previous one. Let’s do some magic:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
class Toolbar_Test { function admin_bar_menu( $wp_admin_bar ) { $node = [ 'id' => 'toolbar_test_lukasz', 'title' => 'Lukasz Nowicki', 'href' => 'http://lukasznowicki.info/', 'meta' => [ 'class' => 'toolbar_test_ln', 'title' => 'Lukasz Nowicki - WordPress blog', ], ]; $wp_admin_bar->add_node( $node ); } function __construct() { add_action( 'admin_bar_menu', [ $this, 'admin_bar_menu' ] ); } } $toolbar_test = new Toolbar_Test(); |
What’s the hell? It is almost the same like before! And there are no group, no child items, no parent… Yeah, yeah, I know, I know. Relax. This is first step. Now we’ve got our parent item (it looks like link now). Then we need to add some child items…
1 2 3 4 5 6 7 8 9 10 11 |
$node = [ 'id' => 'toolbar_test_lukasz_homepage', 'title' => 'Home page', 'href' => 'http://lukasznowicki.info/', 'parent' => 'toolbar_test_lukasz', 'meta' => [ 'class' => 'toolbar_test_ln_childitem', 'title' => 'Lukasz Nowicki home page', ], ]; $wp_admin_bar->add_node( $node ); |
Please notice parent
key in our node array. It is what it looks. Parent id for our item. Let’s build second child:
1 2 3 4 5 6 7 8 9 10 11 |
$node = [ 'id' => 'toolbar_test_lukasz_company', 'title' => 'Company', 'href' => 'https://phylax.pl/', 'parent' => 'toolbar_test_lukasz', 'meta' => [ 'class' => 'toolbar_test_ln_childitem', 'title' => 'Lukasz Nowicki Company', ], ]; $wp_admin_bar->add_node( $node ); |
And we will add third child, without proper url… We will use # as href:
1 2 3 4 5 6 7 8 9 10 11 |
$node = [ 'id' => 'toolbar_test_lukasz_plugins', 'title' => 'Plugins', 'href' => '#', 'parent' => 'toolbar_test_lukasz', 'meta' => [ 'class' => 'toolbar_test_ln_childitem', 'title' => 'Lukasz Nowicki Plugins', ], ]; $wp_admin_bar->add_node( $node ); |
And now, I will add sub-item for our child item! It is also very easy, we just need to change parent id:
1 2 3 4 5 6 7 8 9 10 11 |
$node = [ 'id' => 'toolbar_test_lukasz_plugin_gah', 'title' => 'Google Analytics Head', 'href' => 'https://wordpress.org/plugins/analytics-head/', 'parent' => 'toolbar_test_lukasz_plugins', 'meta' => [ 'class' => 'toolbar_test_ln_childitem_subitem', 'title' => 'Lukasz Nowicki Plugin: Google Analytics Head', ], ]; $wp_admin_bar->add_node( $node ); |
Ok, I will add few more items, will change group href to # and will decay adding action (to make it as a last item) and here are results:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
class Toolbar_Test { function admin_bar_menu( $wp_admin_bar ) { # parent (group) item $node = [ 'id' => 'toolbar_test_lukasz', 'title' => 'Lukasz Nowicki', 'href' => '#', 'meta' => [ 'class' => 'toolbar_test_ln', 'title' => 'Lukasz Nowicki - WordPress blog', ], ]; $wp_admin_bar->add_node( $node ); # first sub-item $node = [ 'id' => 'toolbar_test_lukasz_homepage', 'title' => 'Home page', 'href' => 'http://lukasznowicki.info/', 'parent' => 'toolbar_test_lukasz', 'meta' => [ 'class' => 'toolbar_test_ln_childitem', 'title' => 'Lukasz Nowicki home page', ], ]; $wp_admin_bar->add_node( $node ); # second sub-item $node = [ 'id' => 'toolbar_test_lukasz_company', 'title' => 'Company', 'href' => 'https://phylax.pl/', 'parent' => 'toolbar_test_lukasz', 'meta' => [ 'class' => 'toolbar_test_ln_childitem', 'title' => 'Lukasz Nowicki Company', ], ]; $wp_admin_bar->add_node( $node ); # third sub-item / child group $node = [ 'id' => 'toolbar_test_lukasz_plugins', 'title' => 'Plugins', 'href' => '#', 'parent' => 'toolbar_test_lukasz', 'meta' => [ 'class' => 'toolbar_test_ln_childitem', 'title' => 'Lukasz Nowicki Plugins', ], ]; $wp_admin_bar->add_node( $node ); # first sub-item of sub-item $node = [ 'id' => 'toolbar_test_lukasz_plugin_gah', 'title' => 'Google Analytics Head', 'href' => 'https://wordpress.org/plugins/analytics-head/', 'parent' => 'toolbar_test_lukasz_plugins', 'meta' => [ 'class' => 'toolbar_test_ln_childitem_subitem', 'title' => 'Lukasz Nowicki Plugin: Google Analytics Head', ], ]; $wp_admin_bar->add_node( $node ); # second sub-item of sub-item $node = [ 'id' => 'toolbar_test_lukasz_plugin_ppscc', 'title' => 'Post/Page Specific Custom CSS', 'href' => 'https://wordpress.org/plugins/postpage-specific-custom-css/', 'parent' => 'toolbar_test_lukasz_plugins', 'meta' => [ 'class' => 'toolbar_test_ln_childitem_subitem', 'title' => 'Lukasz Nowicki Plugin: Post/Page Specific Custom CSS', ], ]; $wp_admin_bar->add_node( $node ); # third sub-item of sub-item $node = [ 'id' => 'toolbar_test_lukasz_plugin_qi', 'title' => 'Queen Inspirations', 'href' => 'https://wordpress.org/plugins/queen-inspirations/', 'parent' => 'toolbar_test_lukasz_plugins', 'meta' => [ 'class' => 'toolbar_test_ln_childitem_subitem', 'title' => 'Lukasz Nowicki Plugin: Queen Inspirations', ], ]; $wp_admin_bar->add_node( $node ); # fourth sub-item / second child group $node = [ 'id' => 'toolbar_test_lukasz_github', 'title' => 'Github', 'href' => '#', 'parent' => 'toolbar_test_lukasz', 'meta' => [ 'class' => 'toolbar_test_ln_childitem', 'title' => 'Lukasz Nowicki Plugins', ], ]; $wp_admin_bar->add_node( $node ); # first sub-item of second sub-item $node = [ 'id' => 'toolbar_test_lukasz_github_eba', 'title' => 'Example bulk actions', 'href' => 'https://github.com/lukasznowicki/example-bulk-actions', 'parent' => 'toolbar_test_lukasz_github', 'meta' => [ 'class' => 'toolbar_test_ln_childitem_subitem', 'title' => 'Lukasz Nowicki Github: Example bulk actions', ], ]; $wp_admin_bar->add_node( $node ); # second sub-item of second sub-item $node = [ 'id' => 'toolbar_test_lukasz_github_wab', 'title' => 'WordPress Admin Buttons', 'href' => 'https://github.com/lukasznowicki/wordpress-admin-buttons', 'parent' => 'toolbar_test_lukasz_github', 'meta' => [ 'class' => 'toolbar_test_ln_childitem_subitem', 'title' => 'Lukasz Nowicki Github: WordPress Admin Buttons', ], ]; $wp_admin_bar->add_node( $node ); } function __construct() { add_action( 'admin_bar_menu', [ $this, 'admin_bar_menu' ], 9999 ); } } $toolbar_test = new Toolbar_Test(); |
And this will look like this:
Attaching to existed items
This one is very similar to previous one! I know, you’ve already heard that 😉 We will simple use existing parent id as a parent… and that should works! How to get existing nodes names? It’s easier than you may think. Simply look into HTML source of any page with active WordPress Toolbar. You will find ids started with wp-admin-bar-
. The rest is the id… So, for “New” that would be new-content
(because whole id is wp-admin-bar-new-content
). Ok, you got the point. So, let’s do it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
class Toolbar_Test { function admin_bar_menu( $wp_admin_bar ) { # parent (group) item $node = [ 'id' => 'toolbar_test_lukasz', 'title' => 'Lukasz Nowicki', 'href' => '#', 'parent' => 'new-content', 'meta' => [ 'class' => 'toolbar_test_ln', 'title' => 'Lukasz Nowicki - WordPress blog', ], ]; $wp_admin_bar->add_node( $node ); # first sub-item $node = [ 'id' => 'toolbar_test_lukasz_homepage', 'title' => 'Home page', 'href' => 'http://lukasznowicki.info/', 'parent' => 'toolbar_test_lukasz', 'meta' => [ 'class' => 'toolbar_test_ln_childitem', 'title' => 'Lukasz Nowicki home page', ], ]; $wp_admin_bar->add_node( $node ); # second sub-item $node = [ 'id' => 'toolbar_test_lukasz_company', 'title' => 'Company', 'href' => 'https://phylax.pl/', 'parent' => 'toolbar_test_lukasz', 'meta' => [ 'class' => 'toolbar_test_ln_childitem', 'title' => 'Lukasz Nowicki Company', ], ]; $wp_admin_bar->add_node( $node ); # third sub-item / child group $node = [ 'id' => 'toolbar_test_lukasz_plugins', 'title' => 'Plugins', 'href' => '#', 'parent' => 'toolbar_test_lukasz', 'meta' => [ 'class' => 'toolbar_test_ln_childitem', 'title' => 'Lukasz Nowicki Plugins', ], ]; $wp_admin_bar->add_node( $node ); # first sub-item of sub-item $node = [ 'id' => 'toolbar_test_lukasz_plugin_gah', 'title' => 'Google Analytics Head', 'href' => 'https://wordpress.org/plugins/analytics-head/', 'parent' => 'toolbar_test_lukasz_plugins', 'meta' => [ 'class' => 'toolbar_test_ln_childitem_subitem', 'title' => 'Lukasz Nowicki Plugin: Google Analytics Head', ], ]; $wp_admin_bar->add_node( $node ); # second sub-item of sub-item $node = [ 'id' => 'toolbar_test_lukasz_plugin_ppscc', 'title' => 'Post/Page Specific Custom CSS', 'href' => 'https://wordpress.org/plugins/postpage-specific-custom-css/', 'parent' => 'toolbar_test_lukasz_plugins', 'meta' => [ 'class' => 'toolbar_test_ln_childitem_subitem', 'title' => 'Lukasz Nowicki Plugin: Post/Page Specific Custom CSS', ], ]; $wp_admin_bar->add_node( $node ); # third sub-item of sub-item $node = [ 'id' => 'toolbar_test_lukasz_plugin_qi', 'title' => 'Queen Inspirations', 'href' => 'https://wordpress.org/plugins/queen-inspirations/', 'parent' => 'toolbar_test_lukasz_plugins', 'meta' => [ 'class' => 'toolbar_test_ln_childitem_subitem', 'title' => 'Lukasz Nowicki Plugin: Queen Inspirations', ], ]; $wp_admin_bar->add_node( $node ); # fourth sub-item / second child group $node = [ 'id' => 'toolbar_test_lukasz_github', 'title' => 'Github', 'href' => '#', 'parent' => 'toolbar_test_lukasz', 'meta' => [ 'class' => 'toolbar_test_ln_childitem', 'title' => 'Lukasz Nowicki Plugins', ], ]; $wp_admin_bar->add_node( $node ); # first sub-item of second sub-item $node = [ 'id' => 'toolbar_test_lukasz_github_eba', 'title' => 'Example bulk actions', 'href' => 'https://github.com/lukasznowicki/example-bulk-actions', 'parent' => 'toolbar_test_lukasz_github', 'meta' => [ 'class' => 'toolbar_test_ln_childitem_subitem', 'title' => 'Lukasz Nowicki Github: Example bulk actions', ], ]; $wp_admin_bar->add_node( $node ); # second sub-item of second sub-item $node = [ 'id' => 'toolbar_test_lukasz_github_wab', 'title' => 'WordPress Admin Buttons', 'href' => 'https://github.com/lukasznowicki/wordpress-admin-buttons', 'parent' => 'toolbar_test_lukasz_github', 'meta' => [ 'class' => 'toolbar_test_ln_childitem_subitem', 'title' => 'Lukasz Nowicki Github: WordPress Admin Buttons', ], ]; $wp_admin_bar->add_node( $node ); } function __construct() { add_action( 'admin_bar_menu', [ $this, 'admin_bar_menu' ], 9999 ); } } $toolbar_test = new Toolbar_Test(); |
Now hover your mouse pointer over New
in Toolbar 🙂
Hope you enjoy.