define('DISALLOW_FILE_EDIT', true); February 2011 – unFocus Projects – Kevin Newman and Ken Newman

WordPress Admin Bar Theme Support

Here’s a quickie for WordPress Theme designers:

If your theme is getting unwanted scroll-bars because of the new Admin Bar is WordPress 3.1, the core team included a way to handle it. Add Theme Support for it!

With a full height layout, you’ll want to avoid adding a margin or padding to a height that is already at 100% because you’ll get useless scrollbars, and no one wants that. Instead, find the first non-full height element (usually #header or some-such), and apply the margin there (either the 28px for the height of the admin bar, or add 28 to the existing margin if the element already has one). In the code snippet below I assumed you’d create an element or assign the class ‘admin-bar-fix’ to an existing element.

In your theme’s function file, add the following and modify as you see fit: (best to leave out the closing php tag though)
[cc lang=”php”]
< ?php add_action( 'after_setup_theme', 'custom_theme_setup' ); function custom_theme_setup() { add_theme_support( 'admin-bar', array( 'callback' => ‘admin_bar_bump_callback’) );
}
function admin_bar_bump_callback() { ?>

< ?php } ?>
[/cc]

This snippet is derived from the TwentyTen Theme’s function file. The callback’s original code can be found in the source. Original snippet also found commented in the source (props ocean90).

Basically, by declaring support for the new (as of 3.1) “Admin Bar,” you declare that you can handle how your theme’s content gets “bumped” (by default, it gets pushed down by 28px via a margin on the html.) Most of the time the default behavior is fine… but it’s not fine on theme’s that have a height declaration of 100% (even min-height) or that have external scripts that declare 100% height on the html/body (like Google Translate does).

WordPress’s admin needed a similar treatment but that got patched. The 28px margin is just a default to handle most normal cases. Your theme is your responsibility 🙂

Cheers! Hope this saves some time for someone!

Update: Admin Bar Shim!

If you don’t have a 100% height type of layout but are annoyed by improperly scrolling anchored links try the following.

If you add a >div id=”admin-bar-shim”> (for lack of a better name) in your theme surrounding everything inside the body except the wp_footer call (where the admin-bar gets echoed), you can add
[cc lang=”css”]
.admin-bar #admin-bar-shim {
position: fixed;
bottom: 0pt;
left: 0pt;
right: 0pt;
overflow: auto;
top: 28px;
}
[/cc]
to your style sheet, or use the method above adding the callback. This new method allows anchor links to scroll properly.