Remove WordPress Admin Bar for Specific User Roles

Johan S Dec 22, 2024 User Management
How do I hide the top menu bar from certain users on my website?
What code snippets can I use to programmatically remove the WordPress admin bar for specific user roles in my theme's functions.php file?
Andy answered Dec 22, 2024

Solution 1: Custom Code Implementation

Add this code to your theme's functions.php file or a site-specific plugin to remove the admin bar for specific user roles:

This code removes the admin bar for subscribers and contributors:

add_action('after_setup_theme', 'remove_admin_bar_for_roles');

function remove_admin_bar_for_roles() {
    if (current_user_can('subscriber') || current_user_can('contributor')) {
        show_admin_bar(false);
    }
}

To remove the admin bar for any custom roles, modify the conditional check:

add_action('after_setup_theme', 'remove_admin_bar_for_specific_roles');

function remove_admin_bar_for_specific_roles() {
    // Add or remove roles in this array
    $hidden_admin_bar_roles = array('subscriber', 'contributor', 'custom_role');
    
    $user = wp_get_current_user();
    if (array_intersect($hidden_admin_bar_roles, $user->roles)) {
        show_admin_bar(false);
    }
}

To completely disable the admin bar for all users except administrators:

add_action('after_setup_theme', 'remove_admin_bar_except_admins');

function remove_admin_bar_except_admins() {
    if (!current_user_can('administrator')) {
        show_admin_bar(false);
    }
}

Solution 2: Plugin Options

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

  1. Admin Bar Disabler - Simple plugin to control admin bar visibility per user role
  2. Hide Admin Bar Based on User Roles - Offers granular control over admin bar visibility

Additional Notes

  • The code should be added to your theme's functions.php file or, preferably, a site-specific plugin
  • Changes take effect immediately after adding the code
  • The code uses WordPress core functions and hooks, making it compatible with most themes
  • If using a child theme, add the code to the child theme's functions.php
  • Test the changes while logged in as different user roles to ensure it works as expected

Always back up your site before making code changes to functions.php.