How to Show or Hide Widgets on Specific Pages without Plugin

Do you want to show or hide widgets on specific pages in WordPress? You can do this now. In this post we’ll let you know that how to show or hide widgets on specific pages without plugin.

Now you can show or hide widgets on specific pages without using any plugin. This is tiny snippet which is added by Joel Worsham. Firstly you just need to have the ID name of widget which you want to describe. You have two option either it can be done by viewing page source to view the widget id or you can view it using a web inspector tool as well.

The present sample is setup to show essential ‘pages’ widget on the contact page only. Keep in mind that is_page also is also accepted by an array of page names and id’s.

add_filter( 'widget_display_callback', 'hide_widget_pages', 10, 3 );
function hide_widget_pages( $instance, $widget, $args ) {
  if ( $widget->id_base == 'pages' ) { // change 'pages' to widget name
     if ( !is_page( 'contact' ) ) {    // change page name
         return false;
     }
  }
}

Source