How to Hide the WordPress Admin Bar Programmatically Without Plugin

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 :

wordpress hide admin bar

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.

Similar Articles

  1. How to Access WordPress Admin Dashboard or Login to your WordPress Dashboard

Comments are closed.