Skip to main content

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.phpheader.php if you prefer) to remove this option. (or

For your functions.php

1
2
3
4
5
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

1
2
3
4
5
6
7
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 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
1
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

Comments

Popular posts from this blog

Wrath of The Titans Hollywood Movie 2012 Online Sam Worthington Liam Neeson First Look Poster

Wrath, of, The, Titans, Hollywood, Movie, 2012, Online, Sam, Worthington,  Liam, Neeson, First, Look, Poster Wrath of The Titans Hollywood Movie 2012 Online Sam Worthington  Liam Neeson First Look Poster Wrath of the Titans (2012) Action | Adventure | Fantasy  -   30 March 2012 (USA) Perseus braves the treacherous underworld to rescue his father, Zeus, captured by his son, Ares, and brother Hades who unleash the ancient Titans upon the world. Director: Jonathan Liebesman Writers: Dan Mazeau (screenplay), David Johnson (screenplay), and 4 more  Stars: Sam Worthington, Liam Neeson and Rosamund Pike

John Carter Hollywood Movie 2012 Online Free DVD Taylor Kitsch Lynn Collins First Look Poster

John, Carter, Hollywood ,Movie 2012 ,Online, Free, DVD, Taylor ,Kitsch, Lynn, Collins, First, Look Poster John Carter Hollywood Movie 2012 Online Free DVD Taylor Kitsch  Lynn Collins First Look Poster John Carter (2012) PG_13 132 min  -  Action | Adventure | Fantasy  -   9 March 2012 (USA) Transplanted to Mars, a Civil War vet discovers a lush planet inhabited by 12-foot tall barbarians. Finding himself a prisoner of these creatures, he escapes, only to encounter a princess who is in desperate need of a savior. Director: Andrew Stanton Writers: Andrew Stanton (screenplay), Mark Andrews (screenplay), and 2 more credits » Stars: Taylor Kitsch, Lynn Collins and Willem Dafoe

Real Titanic Pictures From Inside (NEW)

Real, Titanic, Pictures, From, Inside, (NEW), Real Titanic Pictures From Inside (NEW) Scientists have discovered a new type of bacteria eating the remains of iron that slowly eat up 50,000 tons of iron , which is the remains of the sunken ship !! Was named as the new bacteria Halomonas titanicae Halomnas Taitanikaa Scientists expect to eat the whole ship in 20 years! ! Imagine that all of these giant ship that was proud of the achievements of the human will end completely by an enemy so small that we can not see with the naked eye ! !   For more resources on the ill-fated Titanic, try Barnes and Noble promotional codes and save money.