Lorenzo Sfienti

How to Hide WordPress Admin Bar for Specific User Roles

If you want to hide the admin bar for specific user roles while keeping it visible for others, you can achieve this by using code snippets. Follow these steps:

  1. Access your theme’s functions.php file: Connect to your website using an FTP client or access the file manager in your hosting control panel. Locate the functions.php file within your active theme’s directory.
  2. Add the code snippet: Open the functions.php file and add the following code snippet at the bottom:
    // Hide admin bar for specific user roles
    function hide_admin_bar_for_specific_roles($show_admin_bar)
    {
        $current_user = wp_get_current_user();
        $allowed_roles = array('subscriber', 'contributor'); // Add the user roles for which you want to hide the admin bar
    
        if (array_intersect($allowed_roles, $current_user->roles)) {
            return false;
        }
    
        return $show_admin_bar;
    }
    
    add_filter('show_admin_bar', 'hide_admin_bar_for_specific_roles');
  3. Save the changes: Save the modified functions.php file, and the admin bar will be hidden for the specified user roles.