1

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.

1
  • Do you just want to remove the CSS, or the whole admin bar? If you only remove this CSS then the admin bar will overlap the top of the page. Oct 2, 2019 at 2:54

4 Answers 4

1

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

1
  • He wants to remove just the CSS, not the admin bar itself.
    – posfan12
    Apr 2, 2021 at 0:55
1

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

1

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']);
});
0

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;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.