X

Show or Hide widgets on specific pages

Want to show or hide widgets on specific pages? This is a great little snippet contributed by Joel Worsham that will do just that. You will first need to get the id name of the widget that you wish to define. You can do this be viewing the page source or using a web inspector tool to view the widget id. The current sample is setup to only display the core 'pages' widget on the contact page only. Don't forget that is_page also accepts an array of page names and id's.



Code Snippet:


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;
}
}
}

Comments  Leave a Reply

  1. Awesome Joel! I’ll be looking forward to seeing more of what you’ve been working on.

  2. Very useful code. Thanks for sharing.

    1. No problem Sajan, Joel Worsham is going be contributing to wpsnipp.com so I’m sure he will have lots of great snippets to come.

  3. I like it. Thanks for sharing.

    Saves installing another plugin.

    1. Very true brad, nice and simple snippet!

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!