This page looks best with JavaScript enabled

How to hide a woocommerce sidebar widget on certain pages

 ·  ☕ 2 min read  ·  👽 john hashim

So you’re building your own store theme or maybe you’re using a commercial theme and it does make sense for you to load the same products or same information on two places on the same page or maybe you don’t want the widget to show on other pages for some reasons. Remember always try to load less and useful information and resources as possible to speed up your pages and keep your customers happy.

In this simple snippet post will show you how to hide certain widgets on pages depending on your wish.. and to get started … customize this code to meet your need or to fit the widget and the pages your targeting then add it in your functions.php

FYI: If you inspect the sidebar every widget has its own unique id .

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
<?php
add_filter( 'sidebars_widgets', 'woc_hide_widget' );
 
function woc_hide_widget( $sidebars_widgets ) {
    if( ! is_admin() ) {
// you can choose to use  is_cart() or is_page('page')
      if ( is_cart() && is_page('page') ) {
//you have to inspect and find out the widgeet id 
//then replace widget-ID with the id
            $key = array_search( 'widget-ID', 
            $sidebars_widgets['sidebar-1'] );
            if( $key ) {
               unset( $sidebars_widgets['sidebar-1'][$key] );
         }
      }
    }
    return $sidebars_widgets;
}
?>

Remember never add any code snippet on a live or production site, always test it on a stating or local site but if you’re a pro ofcoz you can do it.

Share on

john hashim
WRITTEN BY
john hashim
Web Developer