Disabling/Removing the Toolbar (Admin Bar) in WordPress
First of all, the toolbar can be disabled on the profile page and in the admin panel: Users > Profile.
But when you need to disable it globally, use the function show_admin_bar().
See also:
- show_admin_bar - hook.
- show_admin_bar() - function.
Examples of different disabling options:
Soft Disabling
Suppose we need to disable the "Toolbar" on the front end of the site. However, we need to allow plugins to enable the toolbar through the show_admin_bar filter.
To do this, insert the following line into the theme's functions.php file:
show_admin_bar( false );
Completely Disable the Toolbar
On the front end and in the admin panel:
// Disable admin bar on the front end add_filter( 'show_admin_bar', '__return_false' ); // Disable admin bar in the admin panel remove_action( 'in_admin_header', 'wp_admin_bar_render', 0 );
Disable the Toolbar for Everyone Except Administrators (on the front end)
In this example, the toolbar is not disabled in the admin panel.
add_filter( 'show_admin_bar', 'admin_bar_for_admin_only', 99 );
function admin_bar_for_admin_only( $show_admin_bar ) {
if ( $show_admin_bar && ! current_user_can( 'manage_options' ) ) {
$show_admin_bar = false;
}
return $show_admin_bar;
}
Similarly, you can disable the toolbar for any role. Instead of the capability 'manage_options', specify the appropriate capability for the role, see the list of capabilities here. Here are some of them:
publish_posts- authoredit_others_posts- editor
Disable the Toolbar for Network Administrators
add_filter( 'show_admin_bar', 'admin_bar_for_admin_only', 99 );
function admin_bar_for_admin_only( $show_admin_bar ) {
if ( $show_admin_bar && ! is_network_admin() ) {
$show_admin_bar = false;
}
return $show_admin_bar;
}
And similarly, you can disable the toolbar using conditional tags or other checks.
—
