user3603151
user3603151

Reputation: 101

Remove Woocommerce sidebar from any theme

I'm using WordPress 4.9.4 running Twenty Seventeen Child Theme theme with Woocommerce Version 3.3.4. I am trying to remove the sidebar… I have tried using this:

remove_action('woocommerce_sidebar','woocommerce_get_sidebar',10);

But haven't found the right one yet.

How do I remove all sidebars?

Upvotes: 1

Views: 4285

Answers (3)

Andy P
Andy P

Reputation: 513

Use the is_active_sidebar hook - this should work in any theme as it's core WordPress functionality:

function remove_wc_sidebar_always( $array ) {
  return false;
}
add_filter( 'is_active_sidebar', 'remove_wc_sidebar_always', 10, 2 );

You can also use conditional statements to only hide the sidebar on certain pages, e.g. on Product pages:

function remove_wc_sidebar_conditional( $array ) {

  // Hide sidebar on product pages by returning false
  if ( is_product() )
    return false;

  // Otherwise, return the original array parameter to keep the sidebar
  return $array;
}

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

Upvotes: 0

LoicTheAztec
LoicTheAztec

Reputation: 254383

The best and simple way that works with all themes is to use the get_sidebar Wordpress action hook this way:

add_action( 'get_sidebar', 'remove_woocommerce_sidebar', 1, 1 );
function remove_woocommerce_sidebar( $name ){
    if ( is_woocommerce() && empty( $name ) )
        exit();
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.

You might need to make some CSS changes on some html related containers

This code works on any theme as all themes use get_sidebar() Wordpress function for sidebars (even for Woocommerce sidebar) and get_sidebar action hook is located inside this function code.

Upvotes: 1

user3603151
user3603151

Reputation: 101

WooCommerce ads the side bar in code directed at this specific theme, in class WC_Twenty_Seventeen /** * Close the Twenty Seventeen wrapper. */

public static function output_content_wrapper_end() {
        echo '</main>';
        echo '</div>';
        get_sidebar();
        echo '</div>';
    }

I used this code to replace that function

remove_action( 'woocommerce_after_main_content', array( 'WC_Twenty_Seventeen', 'output_content_wrapper_end' ), 10 );
add_action( 'woocommerce_after_main_content', 'custom_output_content_wrapper_end', 10 );

/** * Close the Twenty Seventeen wrapper. */

function custom_output_content_wrapper_end() {
        echo '</main>';
        echo '</div>'; 
        echo '</div>';
    }

Upvotes: 1

Related Questions