There are many ways to hide the admin bar and many reasons you may want to hide the admin bar. You can do this by adding some code in your theme’s functions.php file.
So, In this tutorial, we will learn how to hide the admin bar in WordPress and also hide the admin bar conditionally.
What is WordPress Admin Bar
WordPress admin bar is on top of the webpage and it will only be shown to logged-in users. This toolbar is visible on both sides WordPress admin side and frontend side.
It shows some shortcuts to jump on that specific page and these shortcuts come from themes, plugins settings if they have been configured. And some also have by default shortcuts.
Sometimes it’s annoying to be visible at the front end because it may be messing with your design and getting a bad user experience. So in this case you may want to get rid of that admin bar.
Let’s get rid of that.
Hide Admin Bar in WordPress
As I said there are many ways to disable the admin bar by just by adding the small piece of code in your theme’s functions.php file.
I recommend using a child theme for any customization in the WordPress theme. It will keep your changes while you have updated your parent theme.
There are many plugins to hide the admin toolbar and you can hide it by just installing the plugin and activate it. But I will recommend doing this with a small code snippet instead of plugins.
Disable the WordPress Admin Bar for All Users
To disable the admin bar for all users including the admin, you have to add the following just one line of code in your theme’s functions.php file.
Add the above code to the file and save it. Now you will see the admin bar is hidden for all users including yourself (admin).
Disable the WordPress Admin Bar for Specific User Roles
You can hide the admin bar conditionally based on the user roles, permissions, or any specific user id. You have to just make the right condition and return the false on condition true.
For disabling the admin bar for specific user roles, add the following code in your active theme’s functions.php file and save it.
In the above code, we made a condition to check the role assigned to the user and hide the WordPress admin bar for subscriber and author roles. You can make it ownself as you want and check any condition.
Disable the WordPress Admin Bar for All Users Except Adminstrators
In this method, that top bar will be hidden for all users but not for administrators. We will make a condition as we did above to check that the user has an admin role or not. If the user will not have an admin role then the condition will be true otherwise false.
To hide the admin bar for all users except admin, add the following code in your theme’s functions.php file and save it.
After adding the above code, you will see the admin bar will be disabled for all users but not for the administrator’s role.
Disable the WordPress Admin Bar Based on User ID
As you see the above examples and now you may know it how to make condition and how to hide the top bar based on any condition term. So in this method, we will see how to hide the admin bar for a specific user ID.
You just know about the what is the user ID for that you want to hide the admin toolbar from the top at the frontend. And check the condition for that ID is the same for logged-in user ID, If it will the same then the condition will be true.
See the following code and add it to your theme’s functions.php file.
In the above code, we declare an array variable holding the users IDs and we checked that the logged-in user id exists in these user’s IDs array. If it exists then the condition will be true and hide the admin bar for that user.
So, as same you can make any condition and check with any terms, roles, ids or anything you want to check. Just check it with the condition and return false for on that condition true.
Hope you understand the all methods. If you still have any questions please ask me in the comment section. I will respond to you as soon as possible.