Categories
Techie stuff

Remove WordPress Admin bar for non admins!

If you’ve got a WordPress website and allow people to sign-up (that includes WooCommerce enabled eCommerce shops), then you probably don’t want them seeing that black admin bar along the top of the browser page – it just doesn’t look right for shoppers/visitors really, and I believe it should only appear for site admins.

There’s a simple snipped of PHP code you can add to your site to get rid of it for non-logged in users, or users who are not Admins. Add the following snippet however you add PHP functions – probably the best and simplest is edit the functions.php file OF YOUR CHILD THEME. Never edit the files of the original theme, as they will be lost on any update. There are plugins which allow you to add PHP functions like below – so you can use those instead.

add_action('after_setup_theme', 'remove_admin_bar');
function remove_admin_bar() {
  if (!current_user_can('administrator') && !is_admin()) {
      show_admin_bar(false);
  }
}

And that’s it! It hooks the function into a standard event, and checks the user has admin rights, if NOT, turn off the admin bar.

By Krispy Brown

Developer (Java / PHP / MS Business Central AL).
Beer brewer and drinker.
DIY Punk promoter.