Overview
Disabling the WordPress admin bar for specific user roles is a common task that can help create a cleaner frontend experience for certain users. Here's how to accomplish this effectively.
Basic Solution
Here's a simple function to hide the admin bar based on user roles:
function disable_admin_bar_for_roles() {
if (current_user_can('subscriber') || current_user_can('customer')) {
show_admin_bar(false);
}
}
add_action('after_setup_theme', 'disable_admin_bar_for_roles');
Advanced Solution with Multiple Roles
A more flexible approach that allows you to specify multiple roles in an array:
function disable_admin_bar_by_role() {
$restricted_roles = array('subscriber', 'customer', 'contributor');
$user = wp_get_current_user();
if (array_intersect($restricted_roles, $user->roles)) {
show_admin_bar(false);
}
}
add_action('after_setup_theme', 'disable_admin_bar_by_role');
Using Filters
A solution using the show_admin_bar
filter for more control:
function custom_show_admin_bar($show) {
if (!is_user_logged_in()) {
return false;
}
$user = wp_get_current_user();
$allowed_roles = array('administrator', 'editor');
return array_intersect($allowed_roles, $user->roles) ? true : false;
}
add_filter('show_admin_bar', 'custom_show_admin_bar');
Best Practices
- Add the code to your theme's
functions.php
file or a custom plugin - Use role-based checks rather than user ID checks
- Consider creating a settings page if you need to change roles frequently
- Test thoroughly after implementation to ensure it works across different user sessions
Security Considerations
- Always validate user capabilities before making changes
- Don't rely solely on hiding the admin bar for security
- Keep in mind that the admin bar is just a UI element
- Use proper WordPress hooks and filters
Common Pitfalls
- Not checking if user is logged in
- Hardcoding user IDs instead of using roles
- Using incorrect hook priority
- Not considering multisite installations
Plugin Solutions
If you prefer a plugin-based solution, here are some reliable options:
-
User Role Editor
- Allows fine-grained control over user roles and capabilities
- Includes admin bar visibility settings
- https://wordpress.org/plugins/user-role-editor/
-
Admin Bar Disabler
- Simple plugin focused solely on admin bar control
- Role-based settings
- https://wordpress.org/plugins/admin-bar-disabler/
CSS Alternative
If you need a quick CSS solution (not recommended for permanent use):
function hide_admin_bar_with_css() {
if (current_user_can('subscriber')) {
echo '<style type="text/css">
#wpadminbar { display:none !important; }
html { margin-top: 0 !important; }
</style>';
}
}
add_action('wp_head', 'hide_admin_bar_with_css');
Testing
After implementing any of these solutions:
- Test with different user roles
- Check both frontend and backend access
- Verify that the admin bar appears correctly for allowed roles
- Clear your browser cache and test in incognito mode
- Test on different devices and screen sizes
Remember that hiding the admin bar is primarily a UI consideration and shouldn't be used as a security measure. Always implement proper user role capabilities for securing your WordPress site.