How to Disable Widgets on Specific Pages in WordPress

Home » Snippets » How to Disable Widgets on Specific Pages in WordPress
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

Are you looking to have more control over the appearance of your WordPress website? Do you want to disable certain widgets on specific pages? Maybe you have a custom landing page that doesn’t require any sidebar content, or perhaps you want to hide a specific widget on a specific page for a more streamlined design. In this article, we’ll show you how to easily disable widgets on specific pages in WordPress, giving you the flexibility to customize your site exactly how you want it.

					function wpturbo_disable_widgets_on_specific_pages() {
    if ( is_page( 'about' ) || is_page( array( 'contact', 'services' ) ) ) {
        unregister_sidebar( 'sidebar-1' );
        unregister_sidebar( 'sidebar-2' );
    }
}
add_action( 'widgets_init', 'wpturbo_disable_widgets_on_specific_pages' );
				

The code snippet provided is a solution for disabling widgets on specific pages in WordPress. This can be useful if you want to have different sidebar content or a different layout on certain pages of your website.

First, let’s take a look at the function that handles disabling the widgets: wpturbo_disable_widgets_on_specific_pages(). This function is hooked to the widgets_init action, so it will be executed when WordPress initializes the widgets.

The if statement inside the function checks if the current page is the "About" page (is_page( 'about' )). If it is, or if the current page is either the "Contact" page or the "Services" page (is_page( array( 'contact', 'services' ) )), the code inside the if statement will be executed.

Inside the if statement, we use the unregister_sidebar() function to unregister the default sidebars with the IDs 'sidebar-1' and 'sidebar-2'. These are the default WordPress sidebars, but the IDs might be different depending on your theme or any additional sidebars you have registered.

By unregistering the sidebars, we are essentially disabling them and preventing them from being displayed on the specified pages. This means that any widgets assigned to these sidebars will not be shown.

To customize this code for your own use, you will need to replace the 'about', 'contact', and 'services' page slugs with the slugs of the pages on which you want to disable the widgets. You can find the page slugs by editing the corresponding pages in the WordPress admin area.

Remember to include this code in your theme’s functions.php file or in a custom plugin file. This will ensure that the code is executed and the widgets are disabled when the widgets are initialized.

That’s it! With this code snippet, you can easily disable widgets on specific pages in WordPress, allowing you to have greater control over the layout and content of your website.

Register an account to save your snippets or go Pro to get more features.