Hide WordPress Admin Toolbar Based On User Role

You can hide the WordPress admin toolbar to logged in users based on their user role and capabilities. First of all you can hide the toolbar on all front end pages from all users by adding to your functions.php

add_filter( 'show_admin_bar', '__return_false' );

Lets say you wanted regular subscriber users not to see the toolbar when logged in your themes functions.php you can add …

if ( ! current_user_can( 'remove_users' ) ) {
 add_filter( 'show_admin_bar', '__return_false' );

The above is saying if the current user does not have the capability to remover users then don’t show the tool bar. A list of roles and user capabilities is found here.

To be more specific on what user roles can see the admin bar you can use current_user_can and pass the user role into it.

add_action('after_setup_theme', 'wl_remove_admin_bar');
// Remove admin bar for subscribers and editors.
function wl_remove_admin_bar() {
 if (get_users( [ 'role__in' => [ 'subscriber', 'editor'] ] ) ) {
   show_admin_bar( false );
 add_action('after_setup_theme', 'themeprefix_disable_admin_bar');
// Show/Hide WP Admin Bar
function themeprefix_disable_admin_bar() {
   if (current_user_can('administrator') || current_user_can('editor') ) {
     // user can view admin bar
   } else {
     // hide admin bar

So in the function above any administrator or editor role will have the WP admin tool bar shown, but for all other roles it will be hidden.


Leave all Comment