WordPress 3.1 introduced Admin Bar to public, before it was just a WordPress.com feature then adopted to self-hosted WordPress blog. This Admin Bar adds an extra menu bar to your WordPress blog depending on the user account level (Role) that you can manage. Last time we gave you tips on how to disable admin bar on WordPress blog, now we will teach you how to add a custom menu and sub-menus to you admin bar.
function my_new_admin_bar_menu() { global $wp_admin_bar; $root_menu = array( 'parent' => false, // parent ID or false to make it root menu 'id' => 'new_custom', // Menu ID should be unique if ROOT menu. 'title' => __('Custom Menu'), // Menu / sub-menu title 'href' => admin_url( 'my-new-menu.php'), // Menu URL 'meta' => false // array of any of the following options: array( 'html' => '', 'class' => '', 'onclick' => '', target => '', title => '' ); ); $wp_admin_bar->add_menu( $root_menu ); } add_action( 'wp_before_admin_bar_render', 'my_new_admin_bar_menu' );
Custom menu wordpress admin bar – functions.php.
Creating sub-menus:
function my_new_admin_bar_menu() { global $wp_admin_bar; $root_menu = array( 'parent' => false, // parent ID or false to make it root menu 'id' => 'new_custom', // Menu ID should be unique. 'title' => __('Custom Menu'), // Menu / Submenu title 'href' => admin_url( 'my-new-menu.php'), // Menu URL 'meta' => false // array of any of the following options: array( 'html' => '', 'class' => '', 'onclick' => '', target => '', title => '' ); ); $sub1 = array( 'parent' => 'new_custom', 'id' => 'new_custom1', 'title' => __('Sub1'), 'href' => admin_url( 'my-first-sub.php'), 'meta' => false ); $sub2 = array( 'parent' => 'new_custom', 'id' => 'new_custom2', 'title' => __('Sub2'), 'href' => admin_url( 'my-second-sub.php'), 'meta' => false ); $wp_admin_bar->add_menu( $root_menu ); $wp_admin_bar->add_menu( $sub1 ); $wp_admin_bar->add_menu( $sub2 ); } add_action( 'wp_before_admin_bar_render', 'my_new_admin_bar_menu' );
Using our first example we will add two sub-menus. We will add a parent value on our array so that it will be a children of our root menu. We will add the parent ID new_custom on all of our sub-menus, make sure you also use a unique ID for all the sub-menus(new_custom1 and new_custom2). Make sure you call again the function $wp_admin_bar->add_menu( $subX ); on all the sub-menu arrays.
Adding sub-menu to existing menus:
function my_new_sub_menu() { global $wp_admin_bar; $sub_menu = array( 'parent' => 'new-content', // parent ID or false to make it root menu 'id' => 'new_custom_sub', // Menu ID should be unique. 'title' => __('Custom Sub Menu'), // Menu / Submenu title 'href' => admin_url( 'my-new-sub-menu.php'), // Menu URL 'meta' => false // array of any of the following options: array( 'html' => '', 'class' => '', 'onclick' => '', target => '', title => '' ); ); $wp_admin_bar->add_menu( $sub_menu ); } add_action( 'wp_before_admin_bar_render', 'my_new_sub_menu' );
As you can see we used new-content as our parent ID, this will be under Add New menu on native WordPress admin bar menu list. Just like on that second example we set the parent ID so that this will be children of the parent menu. Here are some available menu IDs you can use.
– appearance — Appearance menu
– comments — Comments link
– edit — Post/Page/Category/Tag edit menu
– get-shortlink — Shortlink of the page menu
– my-blogs — My Sites menu, if you have more than one site
– new-content — Add New menu
– updates — Update link
That’s it! You can also use this in your next WordPress plugin, if you want to add an admin menu for your settings. You just need to configure it by way your plugin works.