Coding

How to remove the admin bar from a WordPress site

posted on by - 842 views

The admin bar, also known as Toolbar since WordPress 3.3, is a nice and useful feature in WordPress. It’s a floating bar that allows logged in users and administrators to quickly access fundamental resources: there we’ll find a link to the site’s backend, we can add a new post or page, check our user profile, the latest comments, log out and so on.

But we don’t want it there on our website. Maybe we’re building a theme that will show everything in the frontend, with no need to access the administrator panels, and that floating Toolbar up there is just a waste of space; or maybe we’re still working on our theme and want to see our design changes exactly as a logged out guest would see them; or, again, maybe we want it to appear just for administrators, and not for users.

Whatever our case is, let’s see how to hide the WordPress admin bar, for everyone first, and then based on used roles.

How to remove the WordPress admin bar for everyone

This is actually quite easy.

Let’s open our theme’s functions.php and just paste this line of code at the end. Of course, within our php tags.

show_admin_bar( false );

That’s it. Pretty straightforward, isn’t it?

Now, let’s see how to do the same thing, but based on user roles. For example, let’s make the admin bar appear only for administrators.

How to remove the WordPress admin bar for everyone but administrators

We still need that line of code, of course, but this time we first have to check if the current user is a normal user, and not an administrator. So, let’s go again at the end of our functions.php, and then write:

if( !current_user_can( 'administrator' ) ){
    show_admin_bar( false );
}

This way, we make sure that our crucial line of code, the one that hides our admin bar, is only executed if the current user is not an administrator.