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. love this tutorial snippet, thank you!

    unfortunately, i got 2 warnings:
    array_merge(): Argument #2 is not an array
    array_merge(): Argument #1 is not an array

    it did remove the sidebars from conditional page however, even w errors.

    i’m still learning PHP and not sure how to include array $args, if you could please advise… that would be wonderful.

    also wondering if there’s a way to remove 1(one) or more specific widget(s), from specified sidebar without removing all.

    either way, thanks for posting this snippet <3

    1. Are you adding an array to the snippet?

  2. Hey 🙂
    Your code worked for me on all the wordpress pages, but this code didn’t work for me on the WooCommerce store page. I have a basic product search bar widget in the header on every page, but I only want it to be on the STORE page. My woocommerce store page id is 552, so I used this code:

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

    When I add another page id like the About Us page id is 551 …. if I set this id, then your code will hide the product search bar on every site except the About Us page. But when I type the id of the STORE page which is 552, then the search bar is missing from all the pages, also missing from the store page of woocommerce. Can you help me please? Many thanks 🙂

    1. You may want to double check that 552 is the correct id. If it is, you may want to reach out to WooCommerce support.

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

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

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

  6. 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!