How to Disable Admin Bar for All Users Except Site Admin

To disable the admin bar for all users except the site admin in WordPress, you can use the `show_admin_bar` filter along with the `current_user_can()` function to check the user’s capabilities. Here’s an example of how you can achieve this:

 

// Function to disable admin bar for non-admin users
function disable_admin_bar_for_non_admins() {
    if (!current_user_can('administrator') && !is_admin()) {
        show_admin_bar(false);
    }
}
add_action('after_setup_theme', 'disable_admin_bar_for_non_admins');

 

In this example, the `disable_admin_bar_for_non_admins()` function is hooked to the `after_setup_theme` action, which ensures it is executed early in the WordPress initialization process. Inside the function, it checks if the current user does not have the ‘administrator’ capability and is not in the admin area (`is_admin()`). If the user does not meet these conditions, the `show_admin_bar()` function is called with `false` as the argument to disable the admin bar for that user.

By using this code snippet, the admin bar will be hidden for all users except those with the ‘administrator’ role. This provides a clean and focused interface for non-admin users while preserving the admin bar functionality for site admins.

You can place this code in your theme’s `functions.php` file or in a custom plugin. Remember to save the changes and test the functionality while logged in as a non-admin user to ensure the admin bar is appropriately disabled.

Categories

Related Blogs

How to add custom style and script in wordpress

`wp_enqueue_scripts` is a crucial action hook in WordPress that allows you to enqueue or register scripts and stylesheets properly. It is commonly used by theme and plugin developers to load necessary CSS and JavaScript files onto the front-end of a WordPress website.

How to know “Who created this coupon?”

What to do when your client ask “Who created this coupon?” . in WooCommerce.  But we don’t have any default things in WooCommerce  which automatically show author of coupon .

How To add custom product data tabs in WooCommerce

To add custom product data tabs in WooCommerce, you can use the woocommerce_product_data_tabs and woocommerce_product_data_panels hooks. These hooks allow you to add new tabs to the product edit page in the WooCommerce admin area. Here’s a step-by-step guide on how to achieve this: