How to Remove the Admin Bar from WordPress 3.1

You may have noticed that there is a new admin bar add to WordPress 3.1 that shows up when you are logged in as a subscriber or above. While I can see that this might be useful on some sites, for most this is just an annoyance and makes the site look too generically ‘WordPress’ or detracts from existing functionality that you may have added, such as search adaptations.

For example if you have a site that requires people to login before they can make a comment, or login before they can download, this will add to the screen a bunch of ugliness that is totally unwanted. Not to mention a bunch of scripts and codes that will slow the response time of your website.

So with that in mind I have created a bunch of samples that you can add to your functions.php (or header.php if you prefer) to remove this option.

For your functions.php

if (!is_admin() && !current_user_can('add_users')){
	wp_deregister_script( 'admin-bar' );
	wp_deregister_style( 'admin-bar' );
	remove_action('wp_footer','wp_admin_bar_render',1000);
}

For your header.php

<?php
if (!current_user_can('add_users')){
 wp_deregister_script( 'admin-bar' );
 wp_deregister_style( 'admin-bar' );
 remove_action('wp_footer','wp_admin_bar_render',1000);
}
?>

Note: place just before <?php wp_head() ?> in header.php

I’ve only focused on the front-end user experience, rather than both the site and the admin pages.

Code break down….

!is_admin()

!is_admin() checks if this is not an admin page

!current_user_can(‘add_users’)

current_user_can('add_users') checks if the current user can add a user to the WordPress system, this only applies to administrators. So, !current_user_can('add_users') checks if the current user can not add new users, which means all other users. If you want to make the admin bar available to your authors and above then change ‘add_users‘ to ‘publish_posts‘. For a full list of capabilities check out this page on the WordPress Codex.

Do remember that roles and capabilities only apply to logged in users. If you don’t want to see the bar even as an administrator, then remove it from the if statement, or if you are doing this in the header.php remove the if statement all together, leaving it’s contents behind.

wp_deregister_script( ‘admin-bar’ );

wp_deregister_script( 'admin-bar' ); removes the Javascripts that are loaded for the admin bar.

wp_deregister_style( ‘admin-bar’ );

wp_deregister_style( 'admin-bar' ); removes the CSS file from being loaded for the admin bar, as we don’t need to style it.

remove_action(‘wp_footer’,'wp_admin_bar_render’,1000);

remove_action('wp_footer','wp_admin_bar_render',1000); tells the system not to render the admin bar menu (html) when wp_footer is called, the value 1000 means run it after any other wp_footer action that has a lower value, the default is 10.

Want a plugin to do this?

There are a few plugins available to do this for you, BUT they will not be as efficient (read “will slow down your WordPress install silghtly more”) as putting this code directly into your header.php which is the most efficient placement as it only loads for front end pages, or in to your functions.php file which is the given standard. However, here are a list of plugins to do the job if your not confident with playing with php codes:

Admin Bar Disabler →
Custom Admin Bar →
Global Hide/Remove Admin Bar Plugin →
Admin Bar Removal →

Update with cleaner code

thanks to the lovely people at WPUA (WordPress Users Association) I’ve been given an even cleaner bit of code to replace the de-register script and styles and to get rid of the remove_action

add_filter( 'show_admin_bar', '__return_false' );

Of course you can still do all the same !is_admin or !current_user_can stuff if you like

Author: (13 Posts)

Andy Killen has been working exclusively with the web since 1994 when he was Intel's internet engineer for Europe. Today he runs his own company phat-reaction.com as well as is the CTO of Speckyboy and was also part of the Adobe Fireworks CS6 beta test team. When Andy is not busy making websites, themes and plugins or giving training on website performance, wordpress or node.js, he can be found enjoying life in Amsterdam.

Comments