Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am trying to remove the admin bar from a theme front end of a theme.

I found the following code block:

add_filter( 'show_admin_bar', '__return_false' );
remove_action( 'personal_options', '_admin_bar_preferences' );

Which works fine. However I wanted to add a choice for the user, so that I can add a permanent code block to my boilerplate theme, and allow users to toggle the admin bar off and on.

I have successfully added the toggle in the admin area, and called back the value successfully, however, when I test for the value in order to control the callback of the admin bar, the admin bar goes, but the CSS applied to the HTML element (margin-top: 28px !important;) remains, leaving a 28px gap in the top of my theme.

Here is the code block I am using to call the value back, and respond accordingly:

function block_admin(){
    $show = get_option('admin_bar_');
    $show = $show['admin_bar_toggle'];
    if (!$show || $show != 'on'){
        add_filter( 'show_admin_bar', '__return_false' );
        remove_action( 'personal_options', '_admin_bar_preferences' );
    } 
}
add_action('init', 'block_admin');

Suggestion

By requirement, my custom option (using register_setting) is not initialised until the admin_menu hook is launched, whilst the block_admin is launched on init. However, I don't think this is the issue as I don't think get_option is dependent on the setting being registered, but rather the option existing (or not) in the database.

share|improve this question
add comment

5 Answers

Have you saved your settings, so that they do exist in the database?

Also: what if you change this:

if ( ! $show || $show != 'on' ) {

...to this:

if ( ! isset( $show ) || $show != 'on' ) {
share|improve this answer
    
I am sure the if statement is not the problem, as it does do something, and that something is reversible when I toggle the control back and forth. It removes the bar itself, just not the style applied to the html element. –  Mild Fuzz Jul 19 '11 at 19:25
add comment
up vote 1 down vote accepted

Just launching the function at an earlier point solved the issue

 function block_admin(){
    $show = get_option('admin_bar_');
    $show = $show['admin_bar_toggle'];
    fb::log($show,'shoe');
    if (!$show || $show != 'on'){
        add_filter( 'show_admin_bar', '__return_false' );
        remove_action( 'personal_options', '_admin_bar_preferences' );
    } 
}
add_action('after_setup_theme','block_admin');
share|improve this answer
add comment

There is already an per-user-option to disable the admin-bar in the latest Wordpress versions. It can be found in the user profile settings:

…/wp-admin/profile.php (click on your name after logging in)

share|improve this answer
    
I am removing that as well. In short, I don't like the admin bar. –  Mild Fuzz Jul 21 '11 at 10:48
1  
"I don't like the admin bar" Who does? Mostly it also heavily interacts with the theme, resulting in spaces somewhere… –  feeela Jul 21 '11 at 10:50
    
Having it on as default was a big mistake, in my opinion. –  Mild Fuzz Jul 21 '11 at 19:51
add comment

Had you tried show_admin_bar(false)? From quick look at code is should kill admin bar and it is safe to run very early (on load actually).

share|improve this answer
add comment

Not sure that matches you req. But here is plugin to toogle admin bar on off globaly. Why not have a look :)

http://wordpress.org/extend/plugins/global-admin-bar-hide-or-remove/

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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