Disable WordPress Admin Bar for All Users Except Admins

Henrik B. Dec 27, 2024 Admin Customization
How can I hide the admin tools at the top of my site for everyone except me?
What PHP code can I implement to conditionally remove the WordPress admin bar for all user roles except administrators?
Andy answered Dec 27, 2024

Solution to Hide Admin Bar for Non-Administrators

Custom Code Solution

Add this code to your theme's functions.php file or in a site-specific plugin:

This code checks if the current user is not an administrator and removes the admin bar:

add_action('after_setup_theme', function() {
    if (!current_user_can('administrator')) {
        show_admin_bar(false);
    }
});

Alternative version with more granular control over user roles:

add_action('after_setup_theme', function() {
    $allowed_roles = array('administrator');
    $user = wp_get_current_user();
    
    if (array_intersect($allowed_roles, $user->roles)) {
        show_admin_bar(true);
    } else {
        show_admin_bar(false);
    }
});

Where to Place the Code

  1. In your theme's functions.php file
  2. In a site-specific plugin
  3. Using a code snippets plugin like Code Snippets

Plugin Solutions

If you prefer using a plugin, here are reliable options:

  1. Code Snippets - Add the code above without editing theme files
  2. Hide Admin Bar - Simple plugin with settings to control admin bar visibility per role

Notes

  • The code takes effect immediately after adding
  • No configuration needed
  • Works with all themes
  • Preserves admin bar settings when switching themes
  • Compatible with multisite installations