WordPress shows the admin bar (the black toolbar) at the top of the front end to every logged-in user. On a membership site or a WooCommerce shop, subscribers and customers do not need it: it links to a dashboard they have nothing to do in and breaks the design of the page.
This snippet hides the WordPress admin bar for the roles you choose, subscriber and customer by default, and keeps it for editors and administrators. One setting hides it for everyone.
The snippet
<?php
/**
* Hide the WordPress admin bar (toolbar) on the front end for chosen roles.
*/
// ===== Config =====
function dpc_hide_admin_bar_config(): array {
$config = array(
// true = hide it for every logged-in user, administrators included.
'everyone' => false,
// Roles that do not see the admin bar. 'customer' is the WooCommerce role.
'roles' => array( 'subscriber', 'customer' ),
);
// ===== End of config =====
return (array) apply_filters( 'dpc_hide_admin_bar_config', $config );
}
add_filter( 'show_admin_bar', 'dpc_hide_admin_bar', 20 );
function dpc_hide_admin_bar( $show ) {
if ( ! $show || ! is_user_logged_in() ) {
return $show;
}
$config = dpc_hide_admin_bar_config();
if ( ! empty( $config['everyone'] ) ) {
return false;
}
$roles = (array) wp_get_current_user()->roles;
return array_intersect( $roles, (array) $config['roles'] ) ? false : $show;
}
How it works
- WordPress asks the
show_admin_barfilter whether to draw the toolbar. The snippet answers that question only for logged-in users whose roles are in the list. - The role check runs inside the filter, when WordPress has already loaded the current user. The older version of this post called
is_user_logged_in()andwp_get_current_user()directly in the file. Those functions do not exist yet at that point when the code runs from Scripts Organizer or a plugin, so the site stopped with a fatal error. - A user with several roles is matched if any of them is in the list.
- If another plugin already hid the bar, the snippet leaves it hidden.
Add it with Scripts Organizer
In Scripts Organizer go to Code Blocks → Add New and give the block a name. Set Trigger location to Everywhere, tick PHP under Script location, paste the code into the PHP editor, edit the config and publish. Leave Action/Hook empty.
Everywhere loads the code early, but it only adds a filter, so it is safe there. The filter runs when the page is built.
The same code also works unchanged in a small plugin or an mu-plugin.
Customise it
- Other roles: change
roles, for examplearray( 'subscriber', 'contributor' ). Use the role slug, not its display name. - Everyone: set
everyonetotrue. Administrators lose the front-end toolbar too, but still have it in wp-admin. - Turn it off for one user: they can untick Show Toolbar when viewing site under Users → Profile, which works without any code.
Gotchas
- This only hides the toolbar on the front end. WordPress always shows it inside wp-admin, and subscribers can still open
/wp-admin/to edit their profile. Give them a log out link on the site, for example withwp_logout_url(). - To check what a customer sees, log in with a test account that has that role, or use a role switcher plugin on a staging site.
- Page caching plugins usually skip logged-in users, so the change shows up right away. If yours caches pages for logged-in users, clear the cache.
In the Scripts Organizer library Import it with one click from Scripts Organizer.