Do you want to disable the WordPress admin bar for all user roles except administrators?
Well, we have already seen how to hide the admin bar in WordPress for all users, but there are many scenarios when WordPress website owners want to hide the admin bar for non-admin users.
The below code allows you to hide the admin for all users who are not an administrator in WordPress.
// Remove the admin bar from the front end for non-administrator users
if (!current_user_can('administrator') && !is_admin()) {
add_filter( 'show_admin_bar', '__return_false' );
}
You can also use the below code to achieve the same.
add_action('after_setup_theme', 'maverick_hide_admin_bar');
function maverick_hide_admin_bar() {
if (!current_user_can('administrator') && !is_admin()) {
show_admin_bar(false);
}
}