Editor Enhancer - The Best UI for Oxygen Builder
Customer Login
Post Date: June 1, 2020
Posted In: ,

Hide the WP Admin Bar (Top Bar)

When a customer signs in to your website, do you really want them to see the WordPress top bar? Maybe, but often times not.

Let's face it. The WordPress admin bar at the top - shown to logged in users - isn't a good look for ecommerce shops or designers with client user accounts. It comes across as an oversight and unprofessional. Fortunately, we can get rid of it easily with the Code Snippets plugin!

Code Snippets is a powerful tool for quickly adding functions to run in the admin area, on the front-end, or everywhere - and it's FREE from the WordPress repository! It is the perfect replacement for functions.php, which is disabled while using Oxygen Builder. (If you don't already know, Oxygen entirely disables the theme). A word of warning, however: make sure you're activating snippets that you know work without issues. I've heard horror stories from novice users who, in their excitement, have locked themselves out of WP Admin, deactivated post types, and even took down their site.. Oy vey.. Always backup your website before trying new things - especially with a plugin as powerful as this!

Without further ado, the following snippets provide options for you depending on your unique situation. All the snippets are PHP, so make sure to wrap them with the proper tags (<?php ?>) accordingly. If you decide to use Code Snippets (good), no tag wrapping is necessary. To run your snippet simply:

  1. Navigate to Snippets > Add New
  2. Paste the code
  3. Select your options
  4. Click "Save Changes and Activate"

Hide Admin Bar For Everyone

Away with you, Admin Bar! This snippet hides the admin bar for everyone, including yourself.

add_filter('show_admin_bar', '__return_false');

Here, we are simply telling WordPress to not show the admin bar by setting its filter to false.

Hide for Everyone Except Admins

If you're an admin, you'll find yourself using the aptly-named admin bar quite a bit in your work flow to quickly go back and forth between front/back ends. Certainly, use of this utility probably stops with you in most cases. So, let's remove it for all users that are NOT admins.

function hide_admin_bar_from_non_admins() {
    return ! current_user_can( 'administrator' );
}
add_filter( 'show_admin_bar', 'hide_admin_bar_from_non_admins' );

In this case, we tell the filter to use a function - hide_admin_bar_from_non_admins - which will determine whether or not a user is an administrator. The function current_user_can tests a signed in user's privileges to see what they are capable of doing. Requesting administrator will return whether or not the current user has administrator privileges as true or false. And, finally, using the not (!) operator will flip the truthiness to give us the result we want.

If you want to get really specific, check out all the WordPress user privileges.

Other developers will sometimes show this method with several lines and if statements, but I think that's bloated. This single-line method works perfectly and is easy to remember in a pinch.

Wrapping Up

The results are exactly what we needed, and the solutions so simple and elegant. As always please back up your website before trying new things if you're not familiar with them. Even advanced developers will admit that taking backups frequently has prevented sooo many headaches.

Perhaps I'll write an article about reasons to back up your site.. We'll see.

Leave a Reply

Reach out and say "hello!"

Join the Facebook Group
Editor Enhancer - The Best UI for Oxygen Builder
A Product of Ukuwi, LLC
2020 Ukuwi, LLC. All rights reserved.
closecogcommentchevron-downfacebook-squareellipsis-vtrellocopyrightfacebook-official