Home » How To Disable Admin Bar In WordPress Without Plugin

How To Disable Admin Bar In WordPress Without Plugin

Last updated on December 24, 2020 by

If you log in to your WordPress site, by default admin bar added by the WordPress on the Front-end. The admin bar provides you a quick way to jump over the admin panel with some other useful links. But sometimes, we need to disable it for some reason. Let’s see how to disable admin bar in WordPress without a plugin.

An Ideal Place to Add Code in WordPress

Most Important: Add the following code to your child theme’s functions.php file. If you add custom code directly to your parent theme’s functions.php file then it will be wiped entirely when you update the theme.

If you are using a custom theme and if it doesn’t require any update then you can If you are using a custom theme and if it doesn’t require any update then you can directly place the code into wp-content/themes/your-theme/function.php file.

Please also note that our team has tested all codes in WordPress 5.4.1 version with the GeneratePress child theme & Storefront child theme.

01 Hide Admin Bar for All Users

There are many user roles available in WordPress. We can easily remove the admin bar for all users or specific users. Let’s just remove it for all.

add_filter('show_admin_bar', '__return_false');

Place the above code to your function.php file. Then you can go to the Front-end, and check the admin bar, it should be hidden for all users.

02 Hide Admin Bar for All Users Except Admin

Here, We will create a simple function. That will check the provided user roles with a current logged in user roles. If any match found, then we can hide the admin bar for that role. Let’s just do it.

function tf_check_user_role( $roles ) {

    /*@ Check user logged-in */
    if ( is_user_logged_in() ) :

        /*@ Get current logged-in user data */
        $user = wp_get_current_user();

        /*@ Fetch only roles */
        $currentUserRoles = $user->roles;

        /*@ Intersect both array to check any matching value */
        $isMatching = array_intersect( $currentUserRoles, $roles);

        $response = false;

        /*@ If any role matched then return true */
        if ( !empty($isMatching) ) :
            $response = true;        
        endif;

        return $response;

    endif;
}

$roles = [ 'administrator' ];

if ( !tf_check_user_role($roles) ) :
    add_filter('show_admin_bar', '__return_false');
endif;

On the above example, we have passed the ‘administrator’ role to the newly created function tf_check_user_role() with the NOT IF condition so the admin bar will be hidden for all users except the administrator.

03 Hide Admin Bar for Specific User Roles

Mostly, This scenario useful for multi-user roles. We can use the same above function here and just pass more roles in an array as below.

function tf_check_user_role( $roles ) {

    /*@ Check user logged-in */
    if ( is_user_logged_in() ) :

        /*@ Get current logged-in user data */
        $user = wp_get_current_user();

        /*@ Fetch only roles */
        $currentUserRoles = $user->roles;

        /*@ Intersect both array to check any matching value */
        $isMatching = array_intersect( $currentUserRoles, $roles);

        $response = false;

        /*@ If any role matched then return true */
        if ( !empty($isMatching) ) :
            $response = true;        
        endif;

        return $response;

    endif;
}

$roles = [ 'administrator', 'subscriber' ];

if ( tf_check_user_role($roles) ) :
    add_filter('show_admin_bar', '__return_false');
endif;

On the above example, we have passed ‘administrator’, ‘subscriber’ roles to the created function tf_check_user_role() with the IF condition so the admin bar will be hidden for both provided roles.

If you want to hide admin bar for more other roles then you just need to pass roles in this variable $roles = [ 'administrator', 'subscriber' ];.

Let’s suppose you want to hide admin bar for ‘member’, ‘teacher’, and ‘student’ then you need to define $roles variable like this $roles = [ 'member', 'teacher', 'student' ];

Additionally, read our guide:

  1. How to Add Products Per Page Dropdown in WooCommerce
  2. “Sorry, your session has expired. Return to homepage” – WordPress WooCommerce Error
  3. How to Create a Plugin in WordPress from Scratch
  4. ERROR: Cookies are blocked or not supported by your browser
  5. How To Send Custom Emails in WordPress
  6. How to Allow Preview of Draft Post Without Login in WordPress
  7. Import Users From CSV In WordPress Programmatically
  8. How to Redirect Users After Add To Cart In WooCommerce
  9. How to Remove the Category From URL in WordPress
  10. Remove Custom Taxonomy Base From URL In WordPress

That’s it for now. We hope this article helped you to Disable Admin Bar in WordPress Without a Plugin.

Please let us know in the comments if everything worked as expected, your issues, or any questions. If you think this article saved your time & money, please do comment, share, like & subscribe. Thank you in advance 🙂

 
 

Leave a Comment