To hide the WordPress admin bar for all users except the site administrator, you can use the `show_admin_bar` filter in combination with the `current_user_can()` function. Here’s how you can implement it:
// Disable admin bar for users without admin access function hide_admin_bar_for_non_admins() { if (!current_user_can('administrator') && !is_admin()) { show_admin_bar(false); } } add_action('after_setup_theme', 'hide_admin_bar_for_non_admins');
This function hooks into the `after_setup_theme` action, ensuring it runs early during WordPress initialization. It checks if the current user lacks the ‘administrator’ capability and is not in the dashboard.
If those conditions are met, `show_admin_bar(false)` disables the toolbar for that user.
Add this snippet to your theme’s `functions.php` or in a custom plugin. Don’t forget to test it using a non-admin account to verify that the admin bar is hidden appropriately.