Hide WordPress Admin Bar for All Except Admin

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.

 

Leave a Comment

Your email address will not be published. Required fields are marked *

Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.