Sets the display status of the admin bar.
Description
This can be called immediately upon plugin load. It does not need to be called from a function hooked to the ‘init’ action.
Parameters
$show
boolrequired- Whether to allow the admin bar to show.
Source
function show_admin_bar( $show ) {
global $show_admin_bar;
$show_admin_bar = (bool) $show;
}
Changelog
Version | Description |
---|---|
3.1.0 | Introduced. |
The proper method for hiding the Toolbar on the front-end (cannot be disabled on the back-end).
Basic Example
Placing the below line of code in theme’s function.php file will prevent the Toolbar from rendering on the front end of your site.
Selected display
You can also determine for which users the admin bar is shown. For example the following lines will only display the admin bar for users with administrative privileges.
This function can be called immediately from the functions.php or the main plugin file to disable the admin-bar for all users.
If you want to conditionally hide the admin-bar based on the current page or user role, then you can do it in/before the following actions:
Front-End
wp-admin
Full sample
Note:
The
init
action is fired beforewp
andadmin_init
. It’s possible to useshow_admin_bar()
inside theinit
action, but other plugins might overwrite your decision at a later point.I do NOT recommend using the filter
add_filter( 'show_admin_bar', '__return_false' );
in shared plugins! It will create conflicts with other plugins. Only use the filter in your private (child) theme or internal plugins, and stick with theshow_admin_bar()
function in public or shared plugins.Hide adminbar for custom post type, e.g. ‘course‘
With this example we can hide the admin bar for all users, but for users with “edit posts” permissions, they can opt-in to showing the bar via the “Show Toolbar when viewing site” option on their profile.
You can flip the logic so those users can opt-out of showing the bar as well.