Posted on

WooCommerce: Hide the WP Admin Bar for non-Admins

I was playing today with my own checkout design and UX and while testing I realized new customers could access the WP Admin black bar after purchase. So – I said – easy task!

But after 20 minutes of reality (thought it was going to take 1 minute) I finally found out the problem. And as usual, this blog comes with the solution – feel free to use this fix on your own WooSite ๐Ÿ™‚

D'oh! WooCustomers can see the WP Admin Bar...
D’oh! WooCustomers can see the WP Admin Bar…

1. Hide WP Admin Bar: The Theory

WordPress gives us a great filter called “show_admin_bar“. Easy peasy – set it to false and the admin bar is gone:


// Disable admin bar for non admins (this would work on a non-Woo site...)

function bbloomer_hide_admin_bar_if( $show ) {
	if ( /* CONDITION */ ) $show = false;
	return $show;
}

add_filter( 'show_admin_bar', 'bbloomer_hide_admin_bar_if' );

2. Hide WP Admin Bar: The Woo Reality

After the above snippet wouldn’t work on a Woo install, I did a lot of research. Tried other snippets but nothing. So, I said to myself… what if WooCommerce is ALREADY using that filter and I’m trying to edit the behavior of something that Woo is already modifying?

Well… here’s what I found in woocommerce\includes\wc-user-functions.php:


/**
 * Prevent any user who cannot 'edit_posts' (subscribers, customers etc) from seeing the admin bar.
 *
 */

function wc_disable_admin_bar( $show_admin_bar ) {
	if ( apply_filters( 'woocommerce_disable_admin_bar', get_option( 'woocommerce_lock_down_admin', 'yes' ) === 'yes' ) && ! ( current_user_can( 'edit_posts' ) || current_user_can( 'manage_woocommerce' ) ) ) {
		$show_admin_bar = false;
	}

	return $show_admin_bar;
}
add_filter( 'show_admin_bar', 'wc_disable_admin_bar', 10, 1 );

See, something is already using the filter “show_admin_bar”, and what matters the most – the priority specified there is “10”.

Basically I was changing the behavior of the WP Admin Bar, but then WooCommerce was re-changing it after my call – in fact without specifying the priority, my filter got a default priority of “10”, too early to expect Woo NOT to re-change such functionality.

If this is not clear, and you’d rather get the fix – well, not to worry, here it is.

3. Hide WP Admin Bar for non-Admins: WooCommerce PHP Snippet


/**
 * @snippet       WooCommerce Hide WP Admin Bar for non-Admins
 * @how-to        Watch tutorial @ https://businessbloomer.com/?p=19055
 * @sourcecode    https://businessbloomer.com/?p=21213
 * @author        Rodolfo Melogli
 * @testedwith    WooCommerce 2.6.7
 */

function bbloomer_hide_admin_bar_if_non_admin( $show ) {
	if ( ! current_user_can( 'administrator' ) ) $show = false;
	return $show;
}

add_filter( 'show_admin_bar', 'bbloomer_hide_admin_bar_if_non_admin', 20, 1 );

// please note the priority = '20' to make sure we run the filter after Woo's one

Where to add this code?

You can place PHP snippets at the bottom of your child theme functions.php file (before "?>" if you have it). CSS, on the other hand, goes in your child theme style.css file. Make sure you know what you are doing when editing such delicate files - if you need more guidance, please take a look at my free WooCommerce Customization video tutorial.

Does this snippet still work?

Please let me know in the comments if everything worked as expected. I would be happy to revise the snippet if you report otherwise (please provide screenshots). I have tested this code with Storefront theme, the WooCommerce version listed above and a WordPress-friendly hosting on PHP 7.

If you think this code saved you time & money, please join other Business Bloomer supporters and avail of 365 days of WooCommerce benefits. Thank you in advance ๐Ÿ™‚

Need Help with WooCommerce?

Check out these free videos, tutorials and tips!

  • how-to-edit-woocommerce-with-php-snippets
  • woocommerce-hooks-add_action-list-visual
  • woocommerce-customize-single-product-page-PHP
  • woocommerce-customize-shop-page-PHP
  • woocommerce-advanced-customization
  • how-to-edit-woocommerce-cart
  • woocommerce-customize-checkout-page-PHP
  • woocommerce-email-customization
  • woocommerce-conditional-logic

Rodolfo Melogli

Author, WooCommerce expert, WordCamp speaker and Internet marketer, Rodolfo Melogli has worked as a WooCommerce freelancer since 2011. He helps entrepreneurs and developers overcome their WooCommerce nightmares ๐Ÿ™‚ Rodolfo is the organiser of WordCamp Dublin, the Dublin WooCommerce Meetup, the Dublin Ecommerce Meetup and the Dublin WordPress Meetup. He enjoys interacting with people, travelling and chasing tennis & soccer balls. Of course, he loves pizza too.

19 thoughts on “WooCommerce: Hide the WP Admin Bar for non-Admins

  1. Still works July 2018! Thank you so much!

    1. Excellent ๐Ÿ™‚

  2. You are the best!! Thank you so much. Always reliable

    1. Thank you so much ๐Ÿ™‚

  3. My God YOU are the man!!! Thanks sooooo much I have been looking for a solution to this for ages!!
    You’re absolutely the best. Thanks again!

    1. Thank you Valentina ๐Ÿ™‚

  4. Thank you so much!!! Thanks, thanks, thanks….

  5. Hi. Great article. Helped me a bunch. None of the “hide admin bar” plugins I have tried seemed to work. I could hide the admin bar from an “admin” but not from a “customer” doesn’t make much sense.. Your solution works great. Admin can see but customers cannot – just what I wanted.

    I would like to ask, though. Is it possible to add another role in the “whitelist”? Say, for example, I have a Shop Manager who needs to be able to see the admin bar. Can I add another function in child themes functions.php and display the admin bar for both Admins and Shop Managers? Thanks in advance.

    1. Jeff, thanks so much for your comment! Yes, this is possible – but unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. Thanks a lot for your understanding! ~R

    2. Hi Rodolfo.
      Thanks for the quick reply. Much appreciated. I get that and understand completely. I will figure it out, I guess. Again, thanks for the really helpful post. Keep up the great work!

    3. Thanks Jeff ๐Ÿ™‚

    4. Hi Jeff, did you find your solution here ? This could interest me quite a lot ๐Ÿ™‚

  6. Thank you so much, I just couldn’t figure out why none of the WordPress fix was working… Until you gave me the solutions! Had no idea Woocommerce was the culprit,
    Thanks again,
    Capucine

    1. Awesome, thanks Capucine!

  7. Thank you!!!!

    This was driving me totally bonkers.

  8. Worked perfect !

    1. Awesome, thanks for your feedback Demian!

Questions? Feedback? Support? Leave your Comment Now!
If you're writing code, please wrap it between: [php] code_here [/php]

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.