How to remove the Admin Bar in WordPress 3.1 correctly

Although the WordPress Admin Bar can be hidden by a user visiting their profile page in the Admin Panel (Users > Your Profile), there may be a situation where you want to force the removal without instructing the user to amend their settings or updating their settings automatically.

When the issue of removing the Admin Bar first cropped up, the solution was to remove the associated init action, however this is not the correct method of disabling it and should not be used.

remove_action('init', 'wp_admin_bar_init');

Instead, you either use the show_admin_bar filter and simply return false:

function hide_admin_bar() {
    return false;
}
add_filter( 'show_admin_bar', 'hide_admin_bar' );

Or alternatively call the show_admin_bar function directly:

show_admin_bar( false );

If you are disabling the bar from within an init action it is important that you correctly pass the priority value, otherwise some of the assets may remain (such as the JavaScript file) and cause unwanted behaviour.

Using a priority of 9 seemed to be be the most successful from my experiments, although other possibilities may be used:

add_action( 'init', 'my_init_function', 9 );

There are a number of comments in the source code of 3.1, discouraging you from using the previous method of removing the wp_admin_bar_init action, so encourage anyone to update the method they are using.

All of the methods mentioned here can be used in either your plugin file or functions.php file.

5 Replies to “How to remove the Admin Bar in WordPress 3.1 correctly”

  1. I used show_admin_bar( false ); to remove the admin bar successfully. But the javascript /wp-includes/js/l10n.js?ver=20101110 still remains.

    Do you know how to remove this js too? I think this is from the admin bar.

    I tried add_action( ‘init’, ‘my_init_function’, 9 ); but it caused an error on my site and did’t work.

    Like

  2. I found a way to remove the JS. It seems this is for localization, which may or may not be admin bar related.

    wp_deregister_script( ‘l10n’ );

    Like

    1. The method mentioned in this article for removing the admin bar via a theme is not the correct way of removing the admin bar, as stated in /wp-includes/admin-bar.php.

      To hide the admin bar, you’re looking in the wrong place. Unhooking this function will not properly remove the admin bar. For that, use show_admin_bar(false) or the show_admin_bar filter.

      Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: