Remove annoying WP Adminbar CSS

I am trying to remove the annoying WP Admin bar css.

style type="text/css" media="screen"
html { margin-top: 32px !important; }
* html body { margin-top: 32px !important; }
@media screen and ( max-width: 782px ) {
    html { margin-top: 46px !important; }
    * html body { margin-top: 46px !important; }
}
/style

I have tried (according to other OLD answers):

/**
 * Remove admin bar CSS
 */

  function remove_admin_login_header() {
      remove_action('wp_head', '_admin_bar_bump_cb');
  }
  add_action('get_header', 'remove_admin_login_header');

It does not work.

Topic core php theme-development Wordpress

Category Web


The correct way according to a comment in the WordPress source code wp-includes/class-wp-admin-bar.php:60 is to remove the CSS callback via theme support.

This would look like:

add_action('after_setup_theme', function () {
    add_theme_support('admin-bar', ['callback' => '__return_false']);
});

This filter removes the CSS and hides the adminbar on the entire site:

function hide_admin_bar_from_front_end(){
    remove_action( 'wp_head', '_admin_bar_bump_cb' );
    return false;
}
add_filter( 'show_admin_bar', 'hide_admin_bar_from_front_end' );

If you want to disable the CSS only, you must return true instead of false.

If you need it conditionally:

function wpse_maybe_remove_adminbar() {
    if ( your_condition ) {
        add_filter( 'show_admin_bar', 'hide_admin_bar_from_front_end' );
    }
}
add_action('wp_head', 'wpse_maybe_remove_adminbar', 0);

function hide_admin_bar_from_front_end(){
    remove_action( 'wp_head', '_admin_bar_bump_cb' );
    return false;
}

You can disable it for everybody :

add_filter('show_admin_bar', '__return_false');

or do conditionally hide it :

 add_filter('show_admin_bar', 'my_function_to_hide_it');
function my_function_to_hide_it(){
// condition and return true or false 
}

All details there : https://codex.wordpress.org/Function_Reference/show_admin_bar


Good morning!

This filter should work add_filter('show_admin_bar', '__return_false'); right from the wordpress codex.

Alternatively, you can disable it from specific users.enter image description here

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.