Enabling and Disabling WordPress Widget Areas

There seem to be alot of blogs around the net that answer half this question, so while I remember how I did it, I thought I would put pen to paper (so to speak).

The Problem

I'm building a wordpress website of a classic "brochure" style.  That is to say pages for "Home", "About Us", "Our Products", "Another Page" etc... On each of the sub-pages (not the home page), the customer would like to display a column of  images but different images for each page.  The column of images, as you might expect, will be implemented using individual image widgets in the sidebar.

The Solution

I quickly determined that it would be better for me to be able to turn on and off widget areas rather than individual widgets.  This is because I had settled on using an image display widget that displays one image and therefore would  need many occurences of this widget for each page. So, I decided to define 6 widget areas.  Widget-Area-1 would appear on every page and then widget-area-2 to widget-area-6 could be turned on and off for each individual page. So first define the 6 widget areas in your themes function.php


  function SmartWidgets_widgets_init() {
   // Area 1, located at the top of the sidebar.
   register_sidebar( array(
   'name' => __( 'Widget Area 1', 'SmartWidgets' ),
   'id' => 'widget-area-1',
   'description' => __( 'Widget Area 1', 'SmartWidgets' ),
   'before_widget' => '<li id="%1$s" class="widget-container %2$s">',
   'after_widget' => '</li>',
   'before_title' => '<h3 class="widget-title">',
   'after_title' => '</h3>',
  ) );
  // Area 2, located below the Primary Widget Area in the sidebar. Empty by default.
  register_sidebar( array(
  'name' => __( 'Widget Area 2', 'SmartWidgets' ),
  'id' => 'widget-area-2',
  'description' => __( 'Widget Area 2', 'SmartWidgets' ),
  'before_widget' => '<li id="%1$s" class="widget-container %2$s">',
  'after_widget' => '</li>',
  'before_title' => '<h3 class="widget-title">',
  'after_title' => '</h3>',
  ) );
  // Area 3, located in the footer. Empty by default.
  register_sidebar( array(
  'name' => __( 'Widget Area 3', 'SmartWidgets' ),
  'id' => 'widget-area-3',
  'description' => __( 'Widget Area 3', 'SmartWidgets' ),
  'before_widget' => '<li id="%1$s" class="widget-container %2$s">',
  'after_widget' => '</li>',
  'before_title' => '<h3 class="widget-title">',
  'after_title' => '</h3>',
  ) );
  // Area 4, located in the footer. Empty by default.
  register_sidebar( array(
  'name' => __( 'Widget Area 4', 'SmartWidgets' ),
  'id' => 'widget-area-4',
  'description' => __( 'Widget Area 4', 'SmartWidgets' ),
  'before_widget' => '<li id="%1$s" class="widget-container %2$s">',
  'after_widget' => '</li>',
  'before_title' => '<h3 class="widget-title">',
  'after_title' => '</h3>',
  ) );
  // Area 5, located in the footer. Empty by default.
  register_sidebar( array(
  'name' => __( 'Widget Area 5', 'SmartWidgets' ),
  'id' => 'widget-area-5',
  'description' => __( 'Widget Area 5', 'SmartWidgets' ),
  'before_widget' => '<li id="%1$s" class="widget-container %2$s">',
  'after_widget' => '</li>',
  'before_title' => '<h3 class="widget-title">',
  'after_title' => '</h3>',
  ) );
  // Area 6, located in the footer. Empty by default.
  register_sidebar( array(
  'name' => __( 'Widget Area 6', 'SmartWidgets' ),
  'id' => 'widget-area-6',
  'description' => __( 'Widget Area 6', 'SmartWidgets' ),
  'before_widget' => '<li id="%1$s" class="widget-container %2$s">',
  'after_widget' => '</li>',
  'before_title' => '<h3 class="widget-title">',
  'after_title' => '</h3>',
  ) );
  }
  /** Register sidebars by running SmartWidgets_widgets_init() on the widgets_init hook. */
  add_action( 'widgets_init', 'SmartWidgets_widgets_init' );

Next we need a funtion that will turn off a given widget area.  Add this to your theme's function.php


  //register the Shortcode handler
  function xtreme_unregister_sidebar($sidebar) {
   unregister_sidebar($sidebar);
  }

Now is the clever bit, in each page we are going to define a custom variable "sidebar" that will define which sidebars to turn off.  This will be a comma delimited list of sidebar IDs (see the code above where each widget are is assigned an ID 'id' => 'widget-area-6', So in each page define your custom variable sidebar1.jpg We then need to tell the theme to turn off these widget areas.  In your theme's sidebar.php you should find a line like this


<?php get_sidebar(); ?-->

Replace it with the following code;


  <?php
  $sidebarList = get_post_meta($post--->ID, "sidebar", true);
  if ($sidebarList!="") {
  $sidebars=explode(",",$sidebarList);
  foreach ($sidebars as $sidebar) {
  add_action( 'theme_sidebars', 'xtreme_unregister_sidebar' );
  do_action('theme_sidebars',$sidebar);
  }
  }
  get_sidebar();
  ?>

And that should do it! Please note: I'm not a wordpress guru.  I extracted this howto from  a number of different sources and would like to thank all those concerned for their help;

Please let me know if you found this useful or of any improvements by adding a comment below.  Thanks!


Did you know you can hire me?

I take on projects of all sizes. From Consulting to large Development Projects.

If you're starting a new Yii project and would like some help to get setup and running or you need some help with a particular module or you just need someone to develop the whole dang thing, then just ask ...


Beginning Yii ...

I'm very excited about a new Yii Training course that has just been published.

The course is designed for anyone wanting to learn Yii, whether new to frameworks or converting from another.

Eight chapters, each having five videos of between 3 and 5 minutes, take you through the basics of the Yii Framework, building a web application step-by-step.

Save 40% - introductory offer $20.99 / £13.19 / €16.19 / A$20.99

Leave a Comment

twitterfacebookgooglelinkedin https://me.yahoo.com