5+ WordPress Admin bar Tips

Shortlink:

All code input only your active theme functions.php file.

Enable the WordPress Admin Bar for admins only

If the user can not manage options, then do not show them the admin bar.

/*
* Enable the WordPress Admin Bar for admins only
*/
if ( !current_user_can( 'manage_options' ) ) {
remove_action( 'init', '_wp_admin_bar_init' );
}

Display the WordPress Admin Bar in the Admin Area only

/*
* Display the WordPress Admin Bar in the Admin Area only
*/
if ( is_admin() ) {
remove_action( 'init', '_wp_admin_bar_init' );
}

Display the WordPress Admin bar on Websites only


/*
* Display the WordPress Admin bar on Websites only
*/
if ( !is_admin() ) {
remove_action( 'init', '_wp_admin_bar_init' );
}

Disable The Admin Bar within the Network Admin only

This Snip MUST be placed within the must use /mu-plugins/ plugins directory.

/*
* Disable The Admin Bar within the Network Admin only
*/
if ( is_network_admin() ) {
remove_action( 'init', '_wp_admin_bar_init' );
}

Removes the 28px margin for the Admin Bar

 

A disabled Admin Bar leave a 28px space at the top of the page, the snip below removes the extra space. The
example removes the space for both the Admin Area and Websites.

/*
* Removes the 28px margin for the Admin Bar
*/
function remove_adminbar_margin() {
$remove_adminbar_margin = '<style type="text/css">
html { margin-top: -28px !important; }
* html body { margin-top: -28px !important; }
</style>';
echo $remove_adminbar_margin;
}
/* wp-admin area */
if ( is_admin() ) {
remove_action( 'init', '_wp_admin_bar_init' );
add_action( 'admin_head', 'remove_adminbar_margin' );
}
/* websites */
if ( !is_admin() ) {
remove_action( 'init', '_wp_admin_bar_init' );
add_action( 'wp_head', 'remove_adminbar_margin' );
}

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.

Subscribe For Latest Updates

Signup for our newsletter and get notified when we publish new articles for free!