Enforce Admin Bar Visibility to User

In WP there are option to hide or to make Toolbar (formerly Admin Bar) visible for every single user. Generally speaking such feature can be accessed by every single user from the respective profile page (from Administration screen). However, if you as a site administrator want to enforce the visibility of the Admin Bar regardless of user's preferences, you can easily do so.

Enforce to Hide the Admin Bar

In order to hide the Admin Bar for everyone except the site administrator you can use the following snippet.

function enforce_to_hide_admin_bar(){
   if (!current_user_can('manage_options')) {
      return false; } }		

You may also consider using even simpler method for similar functionality. It can get as simple as this one.

<?php if (!current_user_can('manage_options')) { show_admin_bar(false); }?>

On both the cases, I used current_user_can function to check the logged in user's capability. Please check out this page to learn more about the various users and their capabilities.

Enforce to Show the Admin Bar

Just like you can enforce to hide the Admin bar, you can also enforce to make the Admin Bar visible. The method is fairly simple and quite the opposite of what we did earlier.

if (current_user_can('manage_option')) {		
} else {
   show_admin_bar(true); }

All I am doing here is checking the user's capability and then making a decision on whether to display the admin bar or not. Simply copy and past the snippet mentioned above on your current theme's functions.php page and update it. You are good to go.


Commenting is disabled.