Taking Control of WordPress 3.0 Admin Bar

by in WordPress on 24th Feb 2012 · Comments

Remember Google Buzz? Google launched it and pushed it as a default feature in your Gmail accounts regardless of the fact that few might not like it. It was after constant revelation by critics and statistics that Google realized how useless Google Buzz was proving to be. Google Buzz is a thing of the past and Google Plus has taken control of Google’s Social Dreams.

Success or failure of Google Buzz is not the point of discussion for this article. It is the WordPress admin bar which started its journey in somewhat similar fashion. It sticks in my head and irritates me sometimes. I know some (or many?) out there who appreciate the presence of the WordPress Admin Bar, but still. The problem is that the WordPress admin bar is here to stay as it has few good features (when compared to Google Buzz which had none!)

Taking Control of WordPress 3.0 Admin Bar

Today, we optimize our WordPress Admin Bar (like we optimize everything WordPress) and tell ourselves that we have to love it no matter what. Let us “try” and fall in love with the WordPress 3.0 Admin Bar.

NOTE – The WordPress Admin Bar is controlled from the file /wp-includes/admin-bar.php. If you feel like going into the roots of the WordPress Admin Bar then this is the file you should check.

What is WordPress Admin Bar?

Let me clear the air so that all of us are in sync before we face the deluge of WordPress Admin Bar tricks and hacks.

WordPress admin bar was introduced in WordPress 3.1. The purpose of the WordPress Admin Bar was to connect the backend (dashboard) of a WordPress Blog to the Live Blog. It shows up right below the browser address bar and looks somewhat like the one in the image below:

WordPress Admin Bar

The WordPress Admin Bar helps webmasters switch to the basic features of the Dashboard directly from their WordPress blog. Although the presence of the WordPress Admin Bar is a boon, most webmasters (like me) don’t like the presence of anything sitting on our blogs (even when it is visible only for us and not our readers.) I guess the 1-click access to the WordPress Dashboard isn’t what everybody is comfortable with. I still want to type mydomain.com/wp-admin to get to my WordPress Dashboard. Feels geeky!

Why an Admin Bar?

"First step towards a front-end editor" – Matt Mullenweg, the man who gave birth to WordPress.

The WordPress admin bar has been tagged as the first step towards bringing the WordPress editor right into your blog and moving it out of the Dashboard. It will be interesting to see how WordPress incorporates the Dashboard features into our blog’s front end without making it tacky. For now, Admin Bar is here and needs a lot of improvements. Let us, finally, optimize our WordPress admin bar.

Remove Links from the WordPress Admin Bar

There is a lot that you can remove from the WordPress Admin Bar. Before we learn how to add links to the WordPress Admin Bar (which is a bit complex) we will learn how to remove links from the WordPress Admin Bar.

Remove Author Name Menu or just sub-parts

The very first drop down menu in the WordPress Admin Bar is the name of author. It will have two options – Edit Profile and Logout. You can remove this complete menu (or just the sub-parts) by inserting the below piece of code in functions.php of your current theme:

function OXP_admin_bar_edit() {
	global $wp_admin_bar;
	$wp_admin_bar->remove_menu('my-account-with-avatar'); // This (when used individually with other “remove menu” lines removed) will hide the menu item with author name.
	$wp_admin_bar->remove_menu('edit-profile'); // This (when used individually with other “remove menu” lines removed) will remove the sub-part “Edit My Profile”.
	$wp_admin_bar->remove_menu('logout'); // This (when used individually with other “remove menu” lines removed) will remove the sub-part “Logout”.
}
add_action( 'wp_before_admin_bar_render', 'OXP_admin_bar_edit' );

Remove "Dashboard" Link

Next is the Dashboard link in the WordPress Admin Bar. Insert below code in functions.php:

function OXP_admin_bar_edit() {
	global $wp_admin_bar;
	$wp_admin_bar->remove_menu('dashboard');
}
add_action( 'wp_before_admin_bar_render', 'OXP_admin_bar_edit' );

Remove "Add New" menu or just sub-items

Next is the Add New link which has lot of sub items too. Use the below code to remove all or sub items:

function OXP_admin_bar_edit() {
	global $wp_admin_bar;
	$wp_admin_bar->remove_menu('new-content'); // This removes the complete menu “Add New”. You will not require the below “remove_menu” if you using this line.
	$wp_admin_bar->remove_menu('new-post'); // This (when used individually with other “remove menu” lines removed) will hide the menu item “Post”.
	$wp_admin_bar->remove_menu('new-page'); // This (when used individually with other “remove menu” lines removed) will hide the menu item “Page”.
	$wp_admin_bar->remove_menu('new-media'); // This (when used individually with other “remove menu” lines removed) will hide the menu item “Media”.
	$wp_admin_bar->remove_menu('new-link'); // This (when used individually with other “remove menu” lines removed) will hide the menu item “Link”.
	$wp_admin_bar->remove_menu('new-user'); // This (when used individually with other “remove menu” lines removed) will hide the menu item “User”.
	$wp_admin_bar->remove_menu('new-theme'); // This (when used individually with other “remove menu” lines removed) will hide the menu item “Theme”.
	$wp_admin_bar->remove_menu('new-plugin'); // This (when used individually with other “remove menu” lines removed) will hide the menu item “Plugin”.
}
add_action( 'wp_before_admin_bar_render', 'OXP_admin_bar_edit' );

Remove "Comments" menu

As is clear by now, the below piece of code in functions.php will remove the Comments menu item:

function OXP_admin_bar_edit() {
	global $wp_admin_bar;
	$wp_admin_bar->remove_menu('comments');
}
add_action( 'wp_before_admin_bar_render', 'OXP_admin_bar_edit' );

Remove "Appearance" menu or just sub-items

Like we did for Add New, we will remove the complete “Appearance” menu or just the parts of it:

function OXP_admin_bar_edit() {
	global $wp_admin_bar;
	$wp_admin_bar->remove_menu('appearance'); // This removes the complete menu “Appearance”. You will not require the below “remove_menu” if you using this line.
	$wp_admin_bar->remove_menu('new-themes’); // This (when used individually with other “remove menu” lines removed) will hide the menu item “Themes”.
	$wp_admin_bar->remove_menu('new-widgets'); // This (when used individually with other “remove menu” lines removed) will hide the menu item “Widgets”.
	$wp_admin_bar->remove_menu('new-menus'); // This (when used individually with other “remove menu” lines removed) will hide the menu item “Menus”.
}

Remove "Updates" menu item

The below piece of code in your theme’s functions.php will remove the Updates menu item from the WordPress Admin Bar:

function OXP_admin_bar_edit() {
	global $wp_admin_bar;
	$wp_admin_bar->remove_menu('updates');
}

Remove "Shortlink" menu item

The "Shortlink" menu item helps you generate a shortlink for the current page. This can be removed from the WordPress Admin Bar using the below piece of code:

function OXP_admin_bar_edit() {
	global $wp_admin_bar;
	$wp_admin_bar->remove_menu('get-shortlink');
}

Removing the unexpected stuff

The options that I gave you above are the by-default menu items that you see in the WordPress Admin Bar. Sometimes, you will see extra options that lead you to various plugin pages. Actually, it is the plugin that inserts these options into the WordPress Admin Bar. Like, in my case I could see a “Stats” option due to the stats plugin that I have installed.

In such cases you will not find anything in /wp-includes/admin-bar.php file as the plugin uses its own file to insert such custom options. You will have to dig into the plugin files to remove these custom options. All that you have to do is comment out the particular code that inserts these custom options.

In my case the stats plugin had the below piece of code in its stats.php file:

function stats_admin_bar_menu( &$wp_admin_bar ) {
	$blog_id = stats_get_option('blog_id');
	$url = add_query_arg( 'page', 'stats', admin_url() );
	$img_src = add_query_arg(array('noheader'=>'', 'proxy'=>'', 'chart'=>'admin-bar-hours', 'height'=>20, 'hours'=>48), $url);
	$title = __('Views over 48 hours. Click for more Site Stats.', 'stats');
	$menu = array( 'id' => 'stats', 'title' => "<img style='width:95px;height:20px' src='$img_src' alt='$title' title='$title' />", 'href' => $url );
	$wp_admin_bar->add_menu( $menu );
}

In my case the stats plugin had the below piece of code in its stats.php file:

All that I did was enclose the above code in between /* and */ so that it is commented off. Now the custom Stats menu item does not show.

NOTE: This is something that only you can do after searching the plugin files. If you are not comfortable doing this then leave a comment below and I will help you out.

Add Links to WordPress Admin Bar

We have learned to remove almost everything from the WordPress Admin Bar. Let me tell you how you can add new stuff into the WordPress Admin Bar. This will prove beneficial for those who kind-of love the WordPress Admin Bar. Please understand that a lot can be added to the WordPress Admin Bar. I won’t be discussing everything as that will turn this article into an ebook. Rather, let me scratch the basics and introduce the method to you with few examples. The rest will surely be a cakewalk for you.

Add "Visit Site" Link and link to homepage

Although most blog pages have a link to the homepage, if you want to add one then you can use the below piece of code in your functions.php:

function OXP_admin_bar_edit() {
	global $wp_admin_bar;
	$wp_admin_bar->add_menu( array( 
'id' => 'view-site', //the view-site ID that refers to what we are doing.
'title' => __( 'Visit Site' ), //the anchor text that links to homepage.
'href' => home_url() ) ); //the homepage URL to which the anchor text will connect.
}
add_action( 'wp_before_admin_bar_render', 'OXP_admin_bar_edit' );

You have more websites? Add links to those in the Admin Bar

This comes in handy when the Multisite feature of WordPress is enabled in your blog. You will be able to link to other websites under your Multisite features:

function OXP_admin_bar_edit() {
	global $wp_admin_bar;
	$wp_admin_bar->add_menu( array(  
'id' => 'my-blogs', 
'title' => __( 'My Sites' ),  
'href' => admin_url( 'my-sites.php' ) ) );
}
add_action( 'wp_before_admin_bar_render', 'OXP_admin_bar_edit' );

Add sub-menu item in "Add New" menu

This will be interesting. We will add a new item inside a menu that already exists. The below code will add a My Media option in the menu Add New.

function OXP_admin_bar_edit() {
	global $wp_admin_bar;
	$wp_admin_bar->add_menu( array(
		'parent' => 'new-content', // new-content is the ID for “Add New” menu so we use it as parent ID.
		'id' => 'new_media', // You can add any value here as you are adding something very new here.
		'title' => __('Media'), // The anchor text.
		'href' => admin_url( 'media-new.php') // name of file to which you will link to.
}
add_action( 'wp_before_admin_bar_render', 'OXP_admin_bar_edit' );

This wraps up this section. Let me summarize:

  • You use parent ID if you want to add a sub-menu item.
  • Otherwise, you directly use the ID but in both cases you have to include the anchor text and the link as shown in the codes above.

An interesting tutorial to start will be at WP Engineer which teaches you how to integrate the codex search into your WordPress Admin Bar.

Always Show the WordPress Admin Bar

Another innovative way to use the WordPress Admin Bar is to always show the same. This provides your blog with a search bar on the top of your blog which is always present even when you are logged out. You can add various links to this to make it look like a real menu. Also, style it with the CSS of your theme if you want to.

Use the below code to always show the WordPress admin bar with a Log In link to the same. Use other add_menu features (that were explained above) to add other options to your always on WordPress Admin Bar.

function OXP_login_adminbar( $wp_admin_bar) {
 if ( !is_user_logged_in() )
 $wp_admin_bar->add_menu( array( 
'title' => __( 'Log In' ), 
'href' => wp_login_url() ) );
}
add_action( 'admin_bar_menu', 'OXP_login_adminbar' );
add_filter( 'show_admin_bar', '__return_true' , 1000 );

Disabling the WordPress Admin Bar

After all the engineering if you still think that you don’t need the WordPress admin bar then the below section is for you.

Disable Admin Bar at User Level

From the WordPress Dashboard go to User > Your Profile and look for Show Admin Bar section as seen in image below. You can disable the admin bar at user level from this section. This can be a tidy option if you have a lot of users created.

Show Admin Bar for User

Disable Admin Bar for All Users

If you are sure that you don’t want any user to enjoy the comforts of the Admin Bar then you can disable it for all of them by inserting the below piece of code in your theme’s functions.php:

show_admin_bar(false);

Disable Admin Bar for non-Admin Users Only

If you want the admin bar to be disabled for all non-Admin users while the admin continues to access to admin bar then the below code needs to be present in your functions.php:

if (!current_user_can('manage_options')) {
	add_filter('show_admin_bar', '__return_false');
}

Disable Admin Bar for non-Admin Users who aren't Editors too

If you want the editors and admin users to have access to the admin bar while others don’t see the admin bar then use the below code:

if (!current_user_can('edit_posts')) {
	add_filter('show_admin_bar', '__return_false');
}

Using Plugins to Take Control of the WordPress Admin Bar

It is good to use codes to control WordPress features. You get to understand the core of WordPress when you dive into the codes but somehow some webmasters are not comfortable using manual changes to their theme codes. This is when plugins come in handy. Let me quickly take you through the complete list of plugins that will help you control your admin bar.

WP Admin Bar Removal
This plugin will disable the WordPress Admin Bar. Another important feature of this plugin is to enhance the speed of your blog by using minimal memory usage.

Global Hide Admin Bar Plugin
This plugin will add an option to the settings menu using which you can hide the admin bar for all users at once. This plugin will also let you remove the "Show Admin Bar" settings found in Users > Your Profile. With this feature removed, your blog’s users will have no control over the Admin Bar’s visibility.

WP Custom Admin Bar
This plugin, like the above plugin, will let you remove the admin bar at user levels. Another added feature of this plugin is to use custom CSS style for the admin bar. Using this feature you can style the admin bar with CSS similar to that of your blog’s design.

Admin Bar Disabler
Another plugin that will help your disable the WordPress Admin Bar. The unique feature which helps this plugin stand out of the group is support for WordPress Multisite.

Drafts Dropdown
This is a unique plugin. Once enabled, this plugin will add a drafts drop down menu option so that webmasters can quickly access their draft posts right from their WordPress Admin Bar.

Admin Bar Minimiser
This plugin will help you minimize the WordPress Admin Bar with the click of a button. Different from what other plugins do.

Stick Admin Bar to Bottom
Another unique plugin that will push your Admin Bar to the bottom of the browser. This can be used by those webmasters who do not like the idea of the Admin Bar sticking above blog headers but don’t want to remove the Admin Bar.

Snack Bar
This plugin will add a snack menu to the admin bar that will provide quick access to blog/network admin screens for SuperAdmins.

Hide Admin Bar Search
Another unique plugin that will remove the search feature from your WordPress Admin Bar.

PerS Fade Away WordPress Admin Bar
This plugin adds the glossy fade away look to your WordPress admin bar as shown in this screencast.

Conclusions

Those who really want to dig out the WordPress Admin Bar from the core can refer to WordPress below are two WordPress Codex pages:

Lastly, let me know what all have you done with your admin bar. I am very interested to learn more about the admin bar ever since I have started to understand it.

Salman Siddiqui is a seasoned WordPress critic with 5 years of WordPress experience. He is an alpha geek with a degree in computer engineering. He spends his day as software engineer with a Fortune 100 company and nights as a freelance writer. He believes that everyone must blog about something and plans to be a full time writer in future.