How To Hide The WooCommerce Sidebar With Filter Hooks Conditionally

The sidebar is shown all the time with the standard StoreFront Theme. This can really hinder your store by clogging up pages with un-needed distractions. You can hide the sidebar conditionally with filter hooks.

The code snippet is placed in your child theme functions folder. You can use all the conditions as shown, or remove the ones you don’t need such as cart (is_cart), product page ( is_product), and my account (is_page (‘my-account’).

References

is_product () https://docs.woocommerce.com/wc-apidocs/function-is_product.html

is_archive https://codex.wordpress.org/Function_Reference/is_archive

is_cart https://docs.woocommerce.com/wc-apidocs/function-is_cart.html

is_page https://developer.wordpress.org/reference/functions/is_page/


/**
 * 
 * Chimney Rock Themes - Code Snippet
 * @see https://chimneyrockmgt.com/tutorials/how-to-hide-woocommerce-sidebar-with-filter-hooks-conditionally/
 * Hide Sidebar Conditionally
*/

function custom_function_hide_sidebar ( $is_active_sidebar, $index ) {
	if ( $index !== 'sidebar-1' ) {
		return $is_active_sidebar;
	}

	if ( is_product () || is_archive ()  || is_cart () || is_page ('my-account') ) {
		return false;
	} else {
		return $is_active_sidebar;
	}
}

add_filter ( 'is_active_sidebar', 'custom_function_hide_sidebar', 10, 2 );

Hide Without Code

If you would like full control without coding, try one of our themes. We have included all the code for you and you can preview the changes in real time before publishing your content.

Leave a Reply

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