Do you want to hide the WordPress admin bar from subscribers or on the front end?
You can either use a plugin or add a code snippet to your theme to disable WordPress dashboard for subscribers.
Although there are free plugins to hide WordPress admin bar, alternatively you can add a code snippet to your theme or plugin. In this tutorial, we will focus on how to hide the WordPress admin bar programmatically.
Hiding the WordPress admin bar programmatically allows you to use conditional logic such as; disabling the WordPress admin bar for specific users based on their ID or user roles.
WordPress Hide Admin Bar on Dashboard User Profile
There is a default WordPress option that you can use to hide the WordPress admin bar for a specific user when viewing the site. This option is available under the user profile on the WordPress dashboard as shown in the image below :
When this option is unchecked the WordPress admin bar is not displayed in the frontend.
When you just want to hide the WordPress admin bar for one user you can use this as a quick option. This is also an important choice for debugging frontend layout especially when you don’t need the admin bar.
WordPress Hide Admin Bar for All Users
To hide the admin bar for all users you should use the following filter :
add_filter('show_admin_bar', '__return_false');
How the Filter Works
To understand why it works you need to know how the admin bar is displayed in WordPress. In the WordPress Plugin API, there is this filter is responsible for displaying the WordPress admin bar.
The filter above by default is true and therefore shows the admin bar, changing the parameter to removes __return_false
the admin bar for all users as shown in the snippet above. Alternatively, the function can be written in full as follows:
function hide_wordpress_admin_bar(){ return false; } add_filter( 'show_admin_bar' , 'hide_wordpress_admin_bar');
Hide Admin Bar for All Users Expect Administrator
As you can see on the long version the function has a return value of false. We can further use this function’s return value to customize more by hiding the WordPress admin bar to all users apart from the admin as follows :
function hide_wordpress_admin_bar($user){ return ( current_user_can( 'administrator' ) ) ? $user : false; } add_filter( 'show_admin_bar' , 'hide_wordpress_admin_bar');
In the code above we are passing a function param $user
and checking in the return value if the $user
is the administrator and if not we hide the WordPress admin bar.
Hide Admin Bar for Different WordPress User Roles
You can also extend this functionality for multiple roles and hide the admin bar for more than one WordPress user role.
This can be achieved using this function that checks for either administrator or editor role and only shows the admin bar to them and hides for the other users :
function hide_wordpress_admin_bar($hide){ if ( !current_user_can( 'administrator' ) || !current_user_can('editor')) { return false; } return $hide; } add_filter( 'show_admin_bar' , 'hide_wordpress_admin_bar');
Hide Admin Bar for Specific User ID
You can also hide the WordPress admin bar for a specific user using the user id
suppose we have users with the following IDs 24,6,19 and 84, we can put them in an array and then conditionally hide the admin bar for those users as follows :
function hide_wordpress_admin_bar($hide){ $hide_for_users = array( 24, 6, 19, 84); if (in_array( get_current_user_id(), $hide_for_users ) ) { return false; } return $hide; } add_filter( 'show_admin_bar' , 'hide_wordpress_admin_bar');
Conclusion
These code snippets can be easily added to your theme functions.php
file and will work across all WordPress themes. If you are not sure where to add the code, you can get help from a WordPress developer or you can learn more on how to edit functions.php file from here.
Joe is an experienced full-stack web developer with a decade of industry experience in the LAMP & MERN stacks, WordPress, WooCommerce, and JavaScript – (diverse portfolio). He has a passion for creating elegant and user-friendly solutions and thrives in collaborative environments. In his spare time, he enjoys exploring new tech trends, tinkering with new tools, and contributing to open-source projects. You can hire me here for your next project.
Similar Articles
- How to Create WooCommerce Admin Notice for Plugin – WooCommerce Development
- How to Fix Missing Admin Bar Issue in WordPress
- How to Remove WordPress Admin Menu Items Programmatically Without Plugin
- How to Hide Admin Bar for WooCommerce Customers or By User Roles
- How to Reset WordPress Password from PHPMyAdmin
- How to Fix WordPress Admin Dashboard Is Not Displaying Properly & CSS Not Loading
- How to Make a Child Page On WordPress
- How to Set Up WooCommerce Buy One Get One
- How To Display Product Attributes In WooCommerce
- How to Hide Category From Shop Page WooCommerce
- How to Get Product Description & Display WooCommerce
- How to Hide WordPress Theme Name
- How to Login to WooCommerce Admin
- How to Hide Out-of-Stock Products in WooCommerce
- How to Get Admin Email Address in WordPress
- How to Change WooCommerce Checkout Labels
- How to Recover WordPress Password on Localhost
- How to Set Up WooCommerce You Must be Logged in to Checkout
- How to Access WordPress Admin Dashboard or Login to your WordPress Dashboard
Comments are closed.