WooCommerce: Hide/Show The WP Admin Bar

In previous WooCommerce versions, new customers could access the WP Admin black bar after purchase. Now this seems fixed.

Still, what about other user roles, and what if you want to override this default behavior? Well, here’s a quick snippet for you – feel free to use it in your own WooCommerce site. Enjoy!

D'oh! WooCustomers can see the WP Admin Bar...
D’oh! WooCommerce customers used to see the WP Admin Bar after purchase.

1. Hide the 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:

add_filter( 'show_admin_bar', '__return_false' );

2. Hide the WP Admin Bar: the WooCommerce reality

After the above snippet wouldn’t work on a WooCommerce install, I did some 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 WooCommerce 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.
 *
 * Note: get_option( 'woocommerce_lock_down_admin', true ) is a deprecated option here for backwards compatibility. Defaults to true.
 *
 * @param bool $show_admin_bar If should display admin bar.
 * @return bool
 */

function wc_disable_admin_bar( $show_admin_bar ) {
	if ( apply_filters( 'woocommerce_disable_admin_bar', true ) && ! ( 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, they’re 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 (or show) the WP Admin Bar by user role: WooCommerce PHP Snippet

The WooCommerce wc_disable_admin_bar function above disables the WP Admin Bar for all users who cannot “manage WooCommerce” (only admins / store managers can) or “edit posts” (customers, subscribers).

In this example, we want to re-enable the WP Admin Bar for customers:

/**
 * @snippet       Show WP Admin Bar To Customers - WooCommerce
 * @how-to        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 6
 * @community     https://businessbloomer.com/club/
 */

add_filter( 'show_admin_bar', 'bbloomer_show_admin_bar_if_customer', 11, 1 );

function bbloomer_hide_admin_bar_if_non_admin( $show ) {
   if ( wc_current_user_has_role( 'customer' ) ) $show = true;
   return $show;
}

// Please note the priority = '11' to make sure we run the filter after WooCommerce one

Where to add custom code?

You should place custom PHP in functions.php and custom CSS in style.css of your child theme: where to place WooCommerce customization?

This code still works, unless you report otherwise. To exclude conflicts, temporarily switch to the Storefront theme, disable all plugins except WooCommerce, and test the snippet again: WooCommerce troubleshooting 101

Related content

  • WooCommerce: Add Second Description @ Product Category Pages
    In terms of SEO, if you’re trying to rank your product category pages, you really need to make the most of the default WooCommerce product category “description” and “thumbnail”. Most themes, if compatible with WooCommerce, will show this content right below the product category name and above products. Nothing new so far. But what if […]
  • WooCommerce: Add Column to Orders Table @ WP Dashboard
    The WooCommerce Orders Table, which can be found under WP Dashboard > WooCommerce > Orders, provides us with 7 default columns: Order – Date – Status – Billing – Ship to – Total – Actions. This is used by shop managers to have an overview of all orders, before eventually clicking on a specific one. […]
  • WooCommerce: Display Custom Filters @ WP Dashboard > Products
    If you go to WordPress Dashboard > Products you will find default product admin filters such as “Select a category”, “Filter by product type”, “Filter by stock status”. What if you want to add more custom filters to let your shop managers find products easily? For example, you could add “Filter by product tag” (“product […]
  • WooCommerce: Access Thank You Page from Order Admin
    I’ve been testing for over an hour but finally I found a way to make this work. When you are in “Edit Order” view under WordPress Dashboard > WooCommerce > Orders, there is a dropdown of “Order actions”: “Email invoice“, “Resend new order notification“, etc. A major problem I’ve always had while troubleshooting or working […]
  • WooCommerce: Calculate Sales by State
    You’re filing your tax returns and need to know how much you earned in each state… but then find out WooCommerce doesn’t give you this calculation by default within its reports! Don’t worry – today I’ll share a quick snippet so that you can calculate the amount you need in a second. Feel free to […]

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza. Follow @rmelogli

44 thoughts on “WooCommerce: Hide/Show The WP Admin Bar

  1. Worked for me after 5 years! Great post!

  2. Hi,

    how to use the same code for 2 roles, admin + editor for example ?

    Thanks

    1. I believe recently WooCommerce already fixed that. Admins AND editors should be able to see the WP Admin Bar. Is this not the case?

  3. Worked great, thank you

  4. Legend! Works perfectly.

    For those who care and to help this rank, we are running
    Kadence theme, running LearnDash, Woocommerce and Login Press

    1. Great

  5. You’re a genius. Been looking for this for days now.
    Wanted to make the non shop managers to have the admin bar, so I used your code and changed false to true and it worked ! Thanks.

    1. Nice!

  6. didnt work

    1. Hi Karan, thanks for your comment! I just tested this again with Storefront theme and it works perfectly. Maybe your theme (or another plugin) is messing/conflicting with my snippet?

      To troubleshoot, disable all plugins but WooCommerce and also switch temporarily to “Twentyseventeen” theme (load the snippet there in functions.php) – does it work? If yes, you have a problem with your current theme or one of the plugins.

      Hope this helps!

      R

  7. At least on my site running WooCommerce and Flatsome theme, even though the “show toolbar” is automatically checked on Customers, they do not see the bar by default. Something must already be in the code that prevents it for my site at least.

    1. Found it.

      “By default, WooCommerce blocks non-admin users from entering WP Admin, or seeing the WP Admin bar. These areas are usually not relevant to customers and are therefore hidden.”

      Source: https://docs.woocommerce.com/document/allowing-customer-access-to-wp-admin-and-enabling-the-admin-bar/

      1. Cool, thank you Jeff

  8. Not working for me? πŸ™

    1. Matias, thanks for your comment! I just tested this again with Storefront theme and it works perfectly. Maybe your theme (or another plugin) is messing/conflicting with my snippet?

      To troubleshoot, disable all plugins but WooCommerce and also switch temporarily to “Twentyseventeen” theme (load the snippet there in functions.php) – does it work? If yes, you have a problem with your current theme or one of the plugins.

      Hope this helps!

      R

  9. Still working at this date!!! Thanks a lot Rodolfo!

    1. Yay!

  10. Oh THANK YOU Rodolfo!!! I spent hours trying to solve this (various code not working) – and yours is the answer! I so appreciate you posting it.

    1. Excellent πŸ˜‰

  11. Thanks Rodolfo! still works!

    1. Brilliant!

  12. I really never thought about hiding the admin bar for non-admins. When I read the title of your blog, I got surprised that why I never think about this. It can be a great idea if you think from the administrator’s perspective. You have provided a good solution for the admin of the website to hide the admin bar for non-admin users. Thanks for the unique tips.

    1. Great πŸ™‚

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

    1. Excellent πŸ™‚

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

    1. Thank you so much πŸ™‚

  15. 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 πŸ™‚

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

  17. 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. Hi Jeff, did you find your solution here ? This could interest me quite a lot πŸ™‚

  18. 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!

  19. Thank you!!!!

    This was driving me totally bonkers.

  20. Worked perfect !

    1. Awesome, thanks for your feedback Demian!

Questions? Feedback? Customization? Leave your comment now!
_____

If you are writing code, please wrap it like so: [php]code_here[/php]. Failure to complying with this, as well as going off topic or not using the English language will result in comment disapproval. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining the Business Bloomer Club to get quick WooCommerce support. Thank you!

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