Code Snippets

How to Show or Hide Widgets on Specific Pages without Plugin

Riz Ali
Riz
Mar 27, 2015
1 min read
178 words
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

Riz Ali

Riz

Author

Riz is a Senior Web Strategist with over 14+ years of hands-on experience in full-stack web design and digital architecture. Having successfully delivered 250+ web projects, he believes passion is the engine of innovation. Riz specializes in translating complex technical concepts into accessible, fanatical content, helping businesses bridge the gap between aesthetic design and high-performance engineering. His work has been instrumental in scaling digital presence for startups and established brands alike.

View all posts

Continue reading

You might also like

Leave a Reply

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

Menu