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
- In your theme's
functions.php
file
- In a site-specific plugin
- Using a code snippets plugin like Code Snippets
Plugin Solutions
If you prefer using a plugin, here are reliable options:
-
Code Snippets - Add the code above without editing theme files
-
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