Home » WooCommerce: Hide/Show The WP Admin Bar

WooCommerce: Hide/Show The WP Admin Bar

by Tutor Aspire

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 woocommerceincludeswc-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 tutoraspire.com FREE
 * @author        Tutor Aspire
 * @compatible    WooCommerce 6
 * @donate $9     https://www.tutoraspire.com
 */

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

function tutoraspire_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

You may also like