How to Show or Hide Widgets on Specific Pages in WordPress

Snippets by IsItWP

Are you looking for a way to show or hide widgets on specific pages in WordPress? While there’s probably a plugin for this, we have created a quick code snippet that you can use to show or hide widgets on specific pages in WordPress.

First thing you’ll need to do is to get the ID name of the widget that you wish to define. You can do this by 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.


All you have to do is add this code to your theme’s functions.php file or in a site-specific plugin:

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;

Note: If this is your first time adding code snippets in WordPress, then please refer to our guide on how to properly add code snippets in WordPress, so you don’t accidentally break your site.

If you liked this code snippet, please consider checking out our other articles on the site like: 30 ways to make money with online blogging and how to create a WordPress form with a date picker.

Comments  Leave a Reply

  1. Could you let e know how to change the snippet so that I can hide several widgets on specific pages?

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

  3. 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.

  4. I like it. Thanks for sharing.

    Saves installing another plugin.

    1. Very true brad, nice and simple snippet!

Add a Comment Cancel reply

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!
[60% OFF Today!]
[60% OFF Today!]
[10% OFF Today!]
[10% OFF Today!]
[50% OFF Today!]
[50% OFF Today!]