Sunday, April 3, 2011

4 Excellent Techniques To Enhance The WordPress Admin Bar

If you have upgraded to the latest WordPress version (3.1), you will discover that it shows the WordPress Admin bar for you and your logged-in users. While the WP Admin Bar is supposed to make your life easier by providing useful links at the top of the page, it does not provide any form of customization, which crippled its usefulness.
Here are 4 useful techniques you can use to customize and bring out the best of the WP Admin Bar.


1. Remove existing links from WP Admin Bar

Default link:
wp-comment.jpg
1. Navigate to your theme folder and open the functions.php file with a text editor.
2. paste the following code to the end of the file:
add_action( 'wp_before_admin_bar_render', 'remove_admin_bar_links' );
function remove_admin_bar_links() {
        global $wp_admin_bar;
        $wp_admin_bar->remove_menu('comments');
}
The code above will remove the “COMMENTS” link from the Admin Bar. It works by using the functions $wp_admin_bar->remove_menu(ID). By providing the ID of the links that you want to remove (in this case, the ID is COMMENTS), you can remove any links from the Admin Bar.
wp-exist-comment.jpg
Here are some of the ID that will be useful to you:
  • my-account-with-avatar / my-account: Links to your account. The ID depends upon if you have avatar enabled or not.
  • my-blogs: My Sites menu. For network mode only
  • edit: Post/Page edit link
  • new-content: Add New menu
  • comments: Comments link
  • appearance: Appearance menu
  • updates: Updates link
  • get-shortlink: Shortlink to a page

2. Adding custom links to the WP Admin Bar

To add your own links to the Admin bar, append the following code to your functions.php.
add_action( 'wp_before_admin_bar_render', 'add_admin_bar_links' );
function add_admin_bar_links() {
        global $wp_admin_bar;
       $wp_admin_bar->add_menu( array( 
                               'id' => 'Quang.IT', 
                               'title' => __('QuangIT'),  
                               'href' => 'http://quang.it' 
               ));
}
 
wp-menu.jpg 
The example above add “Google.com” as a link in the Admin Bar. You can, of course, customize it to your liking. Things that you need to take note is the ‘id’, ‘title’ and ‘href’
id: the identifier for the link
title: the name that appear on the Admin Bar
href: the url the link is pointing to.
You can also create a submenu with the code below:
add_action( 'wp_before_admin_bar_render', 'add_admin_bar_links' );
function add_admin_bar_links() {
        global $wp_admin_bar;
       $wp_admin_bar->add_menu( array( 
                               'id' => 'QuangIT', 
                              'title' => __('QuangIT'),  
                               'href' => 'http://google.com' 
               ));
       $wp_admin_bar->add_menu( array( 
                               'parent'=>'QuangIT',
                               'id' => 'Quang.IT', 
                               'title' => __('Quang.IT'),  
                               'href' => 'http://en.quang.it' 
               ));
}
This is how it looks like:

wp-submenu.jpg 

3. Show the login form if user is not logged in

By default, the Admin Bar will only appear for logged-in users. If, however, you want to make the admin bar visible for everyone and make it show the login form if the user is not logged in, you can use the WordPress Admin Bar Improved to enable this functionality.

4. Disable the Admin Bar totally

Some of you might dislike the admin bar and wish to remove it totally. Here’s how you can do so.
1. Login to your WordPress dashboard and go to your profile tab.
2. Under the “Show Admin Bar” section, uncheck the option “when viewing site” and “in dashboard“. Save the changes.




wpadminbar-disable.png 


If you are managing a multi-authors blog (or in network mode) and you want to disable the Admin Bar for all your authors, in your functions.php file, copy and paste the following code;
add_filter( 'show_admin_bar', '__return_false' );
This will disable the Admin Bar sitewide.
What other tricks do you use to customize the WP Admin Bar?

No comments:

Post a Comment