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.

<?php
function enforce_to_hide_admin_bar(){
   if (!current_user_can('manage_options')) {
      return false; } }		
add_filter('show_admin_bar','enforce_to_hide_admin_bar');
?>

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.

<?php
if (current_user_can('manage_option')) {		
   show_admin_bar(false); 
} 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.

Today In History

  •  
  •  

Comment

Leave a Reply

Note: Convet HTML, PHP, JavaScripts from Postable, before posting from comment section.
License: By submitting a comment here you grant this site a perpetual license to reproduce your words and name/Web site in attribution. Please use your real name or a pseudonym (i.e., pen name, alias, nom de plume) when commenting. If you add your site name, company name, or something completely random, I'll likely change it to whatever I want.