How can I extend the functions of the WordPress 3.1 admin bar in my plugins?
I am looking for specific hooks and filters to use in adding links or other features to the admin bar.
A good example of what has already been done is the Yoast WordPress SEO (WordPress Plugin).
Currently there is no documentation available on extending the admin bar. According to The Codex there are two filters to turn it off or not show it:
no_admin_bar()
& show_admin_bar()
Yoast’s plug-in is actually a very good example if all you want to do is add menus. Basically, the admin bar is just an alternate set of links to the same plug-in admin pages you have in the sidebar. To add the top-level SEO menu, Yoast does the following:
This adds a menu named “wpseo-menu” to the admin bar and directs users to the plug-in’s dashboard when they click the link. Child links are added in a similar fashion:
You just specify the “parent” of the menu you’re adding.
Then you can go as deep as you need to, calling
$wp_admin_bar->add_menu()
when you need to and specifying the appropriate information.For reference, the variable,
$wp_admin_bar
is an instance of the classWP_Admin_Bar()
within WordPress. It has several different methods and properties, but the one you’re most interested in here is, obviously,add_menu()
. This method accepts certain parameters:array( 'html' => '', 'class' => '', 'onclick' => '', target => '' );
But the rest of the
WP_Admin_Bar()
class is pluggable. It just depends on what exactly you’re trying to do and how you want to do it.See Also:
wpseo_admin_bar()
WP_Admin_Bar()
small example, i had write this also on wp-hackers list before view days
Download the nightly build, and check out these two files;
The class
WP_Admin_Bar
is essentially the ‘API’, whilst the fileadmin-bar.php
uses it to build the default bar and fire off a load of hooks.That’s pretty much the basics – this is all I’ve gathered from a quick gander myself (to be honest, it’s a bit annoying the hook
admin_bar_menu
doesn’t pass back the instance ofWP_Admin_Bar
– I hate all these globals!)