Remove widget areas without editing wordpress template

March 12, 2009 · Print This Article

One thing that’s always bothered you about needing to remove your widgetized areas(Sidebar, most of the time) in WordPress is that you have to dive into template files to do it. For example, you may want to create a post that doesn’t need widgets shown. That typically requires a little editing of your files.

If you don’t feel like editing your theme files, no problem, here is the solution.

What we’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.).


Your choice for site templates and wordpress themes

Comments

Got something to say?

You must be logged in to post a comment.