There are situations when you would like to hide certain widgets from your sidebar or other place when your visitor is on a specific page or post. For instance, I removed the “About me” text widget from the “About” page as the information was redundant and it’s nice to have a clean, fast loading website without any unnecessary data loaded.
You can either do that by using a plugin, which is the easiest and quickest way of doing it or by adding a code in your functions.php (or a custom plugin where you have your settings). If you want to apply this to 1 page and 1 widget only and you’re comfortable editing code, then you can use my snippet code. If you have to hide a few widgets for more pages and posts, then using a plugin would have been easier to manage that.
I will show you how to do it both ways and you can decide based on your situation what’s the best option for you. It’s also good for you to see how it’s done in the code, so you can learn more 😉
1) By using “Display Widgets” WP Plugin
This WordPress Plugin hides widgets on specified pages. It adds checkboxes to each widget to either show or hide it on every site page. With over 200,000 installs (I’ve tested it myself) and a 4.8 out of 5 rating, you can be sure that this plugin does what it claims.
Using it, you can avoid creating multiple sidebars and duplicating widgets by adding checkboxes to each widget in the admin. “Hide on checked pages” is left without any boxes checked, thus all your current widgets will display on all pages.
2) By inserting a code snippet into functions.php
Here’s the code that you can use to remove the widget you want in one or more pages. We will analyse it below:
function bit_remove_widget( $sidebars_widgets ) { global $post; $page_ids = array(2); // List of Page IDs where the widget will get hidden (one or more) $sidebar_id = 'sidebar'; $widget_id = 'text-394376789'; if( in_array($post->ID, $page_ids) ) { foreach($sidebars_widgets[$sidebar_id] as $key => $value) { if($value == $widget_id) { unset($sidebars_widgets[$sidebar_id][$key]); } } } return $sidebars_widgets; } add_filter( 'sidebars_widgets', 'bit_remove_widget' );
By using the WordPress filter ‘sidebars_widgets’, we are removing the ones that we do not need. First, we declare the $page_ids that contains a list with the pages where the widget will be hidden. In my example, I’ve added 2 as the page ID.
To get the page id, you just access the pages list “Pages” – “All Pages” and click on its title. The URL that will contain something like: “/post.php?post=2&action=edit” – The value of “post” (2 in this case) is the Page ID.
Now, we need to get the Sidebar ID and Widget ID. The easiest way for this would be to access “Appearance” – “Widgets” in the Dashboard, then on “Screen Options” (top right), click “Enable accessibility mode”. Once you do, click “Edit” on the widget you want to hide. In the URI, you have the values you need: /widgets.php?widgets-access=on&editwidget=text-394376789&sidebar=sidebar&key=0 (Widget ID and Sidebar ID are shown in bold).
I hope you learned something today and if you have any questions or comments about this technique, feel free to comment below. Many thanks for reading this and looking forward for more WordPress tips and tricks!