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

Titanic The most famous incident in history Women Jobs STC (48498)

Titanic The most famous incident in history in the North Atlantic on April 15, 1912 Belfast is frequently alluded to as " Titanic Town " and there are awesome purposes behind that. No place else on the planet can guarantee a superior relationship with the most popular ship at any point made. It was structured, built and developed in the Belfast shipyard and made its first journey on April 10, 1912. Around then she was hailed as the new miracle of the world and few individuals at that point recognized what lay in front of her. The Titanic was a fabulous blend of Edwardian plan, craftsmanship, and designing.  Belfast during 1909 was one of the world's most noteworthy ports and Harland and Wolff were known as extraordinary compared to other ship manufacturers all through the world. They had a profoundly talented workforce and were perceived as top quality individuals who conveyed a top-notch item. The Titanic in this manner was built to these demanding benchma...

Top 10 Smart Phones in 2012 (IMAGES)

Top, 10, Smart, Phones, in, 2012 Top 10, Smart Phones, in 2012 Top 10 Smart Phones in 2012 Did you know that there are more than 5.5 billion devices Mobile around the world? ! Whether you are rich or poor , big or small , an employee or a student, and regardless of your industry or even your studies, mobile phones have become a necessity used by everyone to become more widespread devices in humans throughout history ! ! What are the best types of these phones in our time ? Very difficult to find a conclusive answer to " best" mobile devices because the comparison is not limited to specifications and possibilities just as much dependence also on the tastes and preferences vary from person to person So if you are considering buying a phone " smart " in the near future, here's a Top 10 smartphones in 2011, So Let Start With 10th : The tenth position -  Dell Venue Pro : Features of this phone runnin...

Most Strangest Accident 9

Most, Strangest, Accident, 9, Most Strangest Accident 9 January 2009: crowding dozens of Italians to obtain the largest amount Possible after the money was a truck transporting About two million euros are coins .. Congestion and cause the street to stop traffic for hours , disrupting access Police of the site until it was too late .. After the birds flew Berzagaha as they say .. The one the police that it was very difficult Determine the amount lost , but may exceed 10 thousand euros.