Disable widget areas (sidebars) without touching theme templates

One thing that’s always bothered me about needing to remove widget areas in WordPress is that you have to dive into template files to do it. For example, I may want to create a post that doesn’t need widgets shown. That typically requires a little editing of my files.

If you know me well enough, then you know that I hate for anyone to edit templates in a WordPress theme, even if that person is me.

So, what I’ll be showing you is two quick and easy ways to disable widget areas using your theme’s functions.php file. Technically, what we’ll be doing is disabling the widgets, which is basically the same thing when you get right down to it.

This article assumes that your theme sets no default code if no widgets are shown. Otherwise, it is outside the scope of this tutorial.

Removing all widget areas

What we’ll be doing in this first function is disabling all widgets on the home page. First, open your theme’s functions.php file and input this PHP code:

<?php

add_filter( 'sidebars_widgets', 'disable_all_widgets' );

function disable_all_widgets( $sidebars_widgets ) {

	if ( is_home() )
		$sidebars_widgets = array( false );

	return $sidebars_widgets;
}

?>

This is a function that will remove widget areas from our home page. Well, you might have other pages, posts, archives, or whatever in mind that you want to disable widget areas on. In that case, you need to look up the appropriate WordPress conditional tag.

Removing a single widget area

Let’s suppose your theme has widgets in multiple places. Let’s further suppose that you want to remove only the footer widget area on single posts. This will be basically the same thing as above, but you need to know the ID of the widget area. This will be something you’ll either have to find in your theme’s code or ask your theme author about.

For the sake of this tutorial, the ID of our footer widget area is simply footer. We’ll create a new function for this as well. Add this to your theme’s functions.php file.

<?php

add_filter( 'sidebars_widgets', 'disable_footer_widgets' );

function disable_footer_widgets( $sidebars_widgets ) {

	if ( is_single() )
		$sidebars_widgets['footer'] = false;

	return $sidebars_widgets;
}

?>

Again, we used a WordPress conditional tag to check if it was a single post.

Why use these methods as opposed to directly editing templates?

One of the biggest issues between theme authors and users happens when it’s time to upgrade a theme. Users don’t generally know the best way to accomplish something like removing a widget area without compromising the original code. Therefore, the user might not upgrade his or her theme and take advantage of the benefits of better code.

WordPress allows a functions.php file in themes that’s ideal for overwriting things. The more we keep users out of theme files, the easier it becomes to upgrade themes for both theme authors and end users.

Also, theme authors should take a good look at the above functions. This is a great way to make certain parts of your theme look and function differently from the rest of the theme. Use your imagination (Ex: pages without sidebars, custom fields to remove widgets, etc.).