Latest blog posts

header ads

How to Hide the WordPress Admin Bar for All Users Using PHP

How to Hide the WordPress Admin Bar for All Users Using PHP

WordPress displays an admin bar at the top of the page for logged-in users by default. While useful for administrators, it may not be necessary for other users. Here’s how to disable it using PHP.

Admin bar example

Why Hide the WordPress Admin Bar?

  • Better User Experience
  • Security & Privacy
  • Customization Needs

Method 1: Disable Admin Bar for All Users

Add the following code to your theme's functions.php file:

add_filter('show_admin_bar', '__return_false');
Disable admin bar code example

Method 2: Hide Admin Bar for All Users Except Administrators

If you want to keep the admin bar for administrators but hide it for others, use this:

add_action('after_setup_theme', function() {
    if (!current_user_can('administrator') && !is_admin()) {
        show_admin_bar(false);
    }
});
        

Method 3: Hide Admin Bar for Specific User Roles

To hide the admin bar only for certain roles, like subscribers and contributors:

add_action('after_setup_theme', function() {
    if (in_array(wp_get_current_user()->roles[0], ['subscriber', 'contributor'])) {
        show_admin_bar(false);
    }
});
        
User roles code example

Conclusion

Hiding the WordPress admin bar can improve user experience and streamline the front-end view. Use the methods above to customize its visibility as needed.

Final result example

Post a Comment

0 Comments