WorPress adds a fixed menu bar

WorPress adds a fixed menu bar at the top of the screen when user is logged in and the menu bar is visible even when you are at the front end of the site so it’s very helpful to access some admin pages quickly. WordPress allows us to customize the bar and it’s possible to add new links (menu items) in that bar or remove or change the existing ones. Using admin_bar_menu or wp_before_admin_bar_render hook we can customize the Admin Bar. To add new items we can call add_menu() method of the $wp_admin_bar global object (an instance of WP_Admin_Bar class) which may available only during the admin_bar_menu andwp_before_admin_bar_render hooks. In most cases developers use WP_Admin_Bar hook (WP_Admin_Bar’s internal hook) but also it could be done using wp_before_admin_bar_render hook as well and this action hook is called before $wp_admin_bar object is used to render the admin bar to the screen.

Note: The Admin Bar is replaced with the toolbar since WordPress Version 3.3. The preferred way to add or remove items to/from the toolbar is with add_node()add_group() and remove_node(), which are almost similar.

How to use add_menu() :

01 add_action( 'admin_bar_menu''handler_function_name' );
02 function handler_function_name()
03 {
04    global $wp_admin_bar;
05    $wp_admin_bar->add_menu( array(
06             'id' => ''// an unique id (required)
07             'parent' => ''// false for a top level menu
08             'title' => ''// title/menu text to display
09             'href' => ''// target url of this menu item
10             // optional meta array
11             'meta' => array(
12                 'onclick' => '',
13                 'html' => '',
14                 'class' => '',
15                 'target' => '',
16                 'title' => ''
17             )
18     ) );
19 }

How to use remove_menu() :

1 add_action( 'wp_before_admin_bar_render''remove_handler' );
2 function remove_handler()
3 {
4     global $wp_admin_bar;
5     $menu_id 'new-user'// the menu id which you want to remove
6     $wp_admin_bar->remove_menu($menu_id);
7 }

Add a new top level menu using wp_before_admin_bar_render hook :

01 add_action( 'wp_before_admin_bar_render''customize_admin_bar' );
02 function customize_admin_bar()
03 {
04     global $wp_admin_bar;
05     $wp_admin_bar->add_menu( array(
06         'id' => 'my_plugin-menu',
07         'parent' => false,
08         'title' => 'WP Fiddle',
09         'href' => admin_url('admin.php?page=wp_fiddle_settings'),
10     ) );
11 }

Add a new top level menu and two sub menus using admin_bar_menu hook :

01 add_action( 'admin_bar_menu''customize_admin_bar' );
02 function customize_admin_bar()
03 {
04     global $wp_admin_bar;
05     $wp_admin_bar->add_menu( array(
06         'id' => 'my_custom-menu',
07         'title' => 'My Menu',
08         'href' => false
09     ) );
10
11     // Sub menu to open one of my plugins page
12     $wp_admin_bar->add_menu( array(
13         'id' => 'my_plugin-page',
14         'parent' => 'my_custom-menu',
15         'title' => 'Plugin Setup',
16         'href' => admin_url('admin.php?page=wp_fiddle'),
17     ) );
18
19     // Sub menu to open facebook (external link) in new window
20     $wp_admin_bar->add_menu( array(
21         'id' => 'facebook-page',
22         'parent' => 'my_custom-menu',
23         'title' => 'Facebook',
24         'href' => 'http://facebook.com',
25         'meta' => array'target'=>'_blank' )
26     ) );
27 }

The ability to customize the admin menu bar is very useful and common use to WordPress plugin authors to add a shortcut link of their plugin’s setup page in the admin bar just like I’ve used in the above example to show my plugin’s setup page using the admin_url() function to set the url of my plugin’s setup page.

Make an Existing Child Node a Parent Node using admin_bar_menu hook :

1 add_action( 'admin_bar_menu''make_parent_node', 999);
2 function make_parent_node( $wp_admin_bar ) {
3     $args array(
4         'id' => 'new-post'// id of the existing child node
5         'title' => 'Add New Post'// Set new title
6         'parent' => false // make it a top level node
7     );
8     $wp_admin_bar->add_node($args);
9 }

Above code will move out the new post link (titled as Post) from the parent menu New and also the title of the link will be changed from Post to Add New Post.

Remove WordPress logo using wp_before_admin_bar_render hook :

1 add_action( 'wp_before_admin_bar_render''remove_the_logo' );
2 function remove_the_logo() {
3     global $wp_admin_bar;
4     
5     // Remove the WordPress logo
6     $wp_admin_bar->remove_menu('wp-logo');
7     // or remove the user menu from 'New' using remove_node()
8     $wp_admin_bar->remove_node('new-user');
9 }

To remove an existing item from admin bar admin_bar_menu won’t work instead, wp_before_admin_bar_renderhook has to be used.

Also, you can completely hide the admin bar from front end (just place this code in functions.php file)

1 // Using "show_admin_bar" filter
2 add_filter('show_admin_bar''__return_false');
3 // Or directly call
4 show_admin_bar( false );

Also, you can conditionally hide the admin bar depending on the user.

1 if ( ! current_user_can( 'manage_options' ) ) {
2     show_admin_bar( false );
3 }

These lines will only display the admin bar for users with administrative privileges.

Also, you can use set_current_user action hook to hide the admin bar

1 add_action('set_current_user''hide_admin_bar');
2 function hide_admin_bar() {
3     if ( !current_user_can('manage_options') ) {
4         show_admin_bar( false );
5     }
6 }

This function uses do_action(), it calls set_current_user hook after setting the current user. This function could be replaced via plugins. If plugins do not redefine this function, then this will be used instead.

You may change the Howdy to Hello or whatever you want, on admin bar

1 add_filter( 'admin_bar_menu''howdy_to_hello', 25 );
2 function howdy_to_hello( $wp_admin_bar ) {
3     $my_account $wp_admin_bar->get_node('my-account');
4     $newtitle str_replace'Howdy,''Hello!'$my_account->title );
5     $wp_admin_bar->add_node( array(
6         'id' => 'my-account',
7         'title' => $newtitle,
8     ));
9 }

If you want to completely remove my-account menu from admin bar, try this

1 add_action( 'wp_before_admin_bar_render''remove_my_account' );
2 function remove_my_account() { 
3     global $wp_admin_bar
4     $wp_admin_bar->remove_menu('my-account'); 
5

Credit : http://heera.it/

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s