0

I am using the following code to remove the admin bar (client request).

add_filter( 'show_admin_bar', '__return_false' );

Trouble is, it leaves some autogenerating CSS, which places a massive white line at the top of my page in its place (via a 28px top margin rule). How can I turn this off as well?

3
  • You could use jQuery to control the css, i guess
    – Xavier
    Apr 4, 2011 at 10:54
  • I'm echoing @anu, please go back and review your questions and please reward the people who took the time to give you an answer by accepting the best answer and up-voting any helpful answers. See paragraphs 3 & 4 after the header "How do I ask a Question" Apr 5, 2011 at 5:34
  • 1
    Could you trace where exactly extra CSS comes from? What file or script? From looking over code that __return_false snippet should kill all CSS and JS that admin bar uses.
    – Rarst
    Apr 5, 2011 at 15:08

4 Answers 4

2

To completely remove the Admin bar deregister the js and css using wp_degregister_script and remove the action.

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);
}
5
  • The script is part of a toggle in the admin area, so different users can turn it on and off (why is this not default behaviour?). Would I run into problems reinstating the admin bar using this method?
    – Mild Fuzz
    Apr 4, 2011 at 12:58
  • Also, strangely, my hack to fix the issue has been to remove the wp_head, am I to assume that the admin bar attaches to the footer? Because my hack (albeit ill advised) works well.
    – Mild Fuzz
    Apr 4, 2011 at 12:59
  • The Admin Bar hooks into wp_footer(), rather than into wp_head(). Note: removing either of these hooks is a bad idea. Apr 4, 2011 at 16:51
  • I did say that :)
    – Mild Fuzz
    Apr 5, 2011 at 8:53
  • This will not show the admin bar, but it will keep the top 28 pixel margin which was the original problem.
    – Jan Fabry
    Apr 8, 2011 at 12:46
2

I think you added this filter too late (on init?), so that the initialization does happen, but the rest does not.

The three places that check show_admin_bar (via is_admin_bar_showing()) are:

Because _wp_admin_bar_init() is called on init, you will be too late if you also add the show_admin_bar filter on init, unless you change the priority.

This will not render the admin bar, but still add the 28px margin:

add_filter( 'init', 'wpse13875_init' );
function wpse13875_init()
{
    add_filter( 'show_admin_bar', '__return_false' );
}

This will completely disable the admin bar, because it will be executed before _wp_admin_bar_init():

add_filter( 'init', 'wpse13875_init', 9 );
function wpse13875_init()
{
    add_filter( 'show_admin_bar', '__return_false' );
}
1
  • This solved it for me.
    – supertrue
    Jun 14, 2012 at 21:28
0

see this answer, was a complete solution including unload the scripts and styles. The hook remove the admin bar, but not the load of js and css.

-1

Can't you just override the CSS - probably the easiest way - add a display: none to whatever the admin bar selector is

1
  • The admin bar css is dynamically added to the *html element with javascript. It will override any attempt to override it.
    – Chris_O
    Apr 4, 2011 at 11:32

Your Answer

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

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