Skip to content

Instantly share code, notes, and snippets.

@galbaras
Last active Aug 29, 2015
Embed
What would you like to do?
Toggling the display of the WordPress Admin Bar (wpadminbar)
The WordPress admin bar can sometimes get in the way of checking what a site looks like
while being logged in, so it's convenient to hide it occasionally.
To do this, we need a CSS class and some jQuery code, which toggles the admin bar display
when pressing Shift-A.
The CSS can be added to the theme's normal stylesheet. The PHP/jQuery code can be added
into footer.php or included within an action in functions.php.
* The code was adapted from the Bottom Admin Bar plugin.
html.no-admin-bar {
margin-top: 0 !important;
}
<?php if ( is_user_logged_in() ) { ?>
// Toggle admin bar display with Shift-A
jQuery(document).ready(function($) {
$('body').keydown(function( event ) {
if ( event.shiftKey === true && event.which === 65 ) {
$('#wpadminbar').toggle();
$('html').toggleClass('no-admin-bar');
}
});
});
<?php } ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment