[wp-trac] Re: [WordPress Trac] #4280: Allow to constrain widgets being displayed on certain page types only

WordPress Trac wp-trac at lists.automattic.com
Thu Jul 3 15:20:57 GMT 2008

#4280: Allow to constrain widgets being displayed on certain page types only
 Reporter:  torbens                  |        Owner:  mufasa  
     Type:  enhancement              |       Status:  reopened
 Priority:  normal                   |    Milestone:  2.7     
Component:  Administration           |      Version:  2.2     
 Severity:  normal                   |   Resolution:          
 Keywords:  has-patch needs-testing  |  
Comment (by DD32):

 A new theme i'm designing will work best with blog related widgets only
 showing on the blog pages, and certain other widgets on pages & the
 homepage, The way i've achieved it for myself is to register multiple

 for example: "Left Post Sidebar" & "Left Homepage Sidebar".
 in theme's function.php:
 add_action('init', 'theme_sidebars');
 function theme_sidebars() {
                                         'name' => 'Left Posts sidebar',
                                         'id' => 'left-posts-sidebar'
                                         'name' => 'Left Home sidebar',
                                         'id' => 'left-home-sidebar'
 in sidebar.php:
                 $sidebar = is_home() ?
                                         'left-posts-sidebar' :

 Thats all good, Except I now have the problem of certain widgets being
 tied to only being on one sidebar(ie. Meta & search).

 So, In order to achieve that, I added this to my themes functions.php:
 add_filter('option_sidebars_widgets',  'theme_filter_active_widgets');
 function theme_filter_active_widgets($value){
         global $sidebar, $theme_widget_backup;
         if ( ! empty($sidebar) && is_admin() ) {
                 foreach ( (array)$value as $key => $bar ) {
                         if ( $key != $sidebar ) {
                                 $theme_widget_backup[ $key ] = $bar;
                                 $value[$key] = array();
         return $value;

 function theme_filter_active_widgets_update($newvalue){
         global $theme_widget_backup;
         if( ! empty($theme_widget_backup) )
                 foreach( (array)$theme_widget_backup as $key => $value )
                         $newvalue[$key] = array_merge(
 (array)$newvalue[$key], $value);

         return $newvalue;
 In short, It hides whats on the other sidebars from the widgets admin,
 Allowing me to add widgets to multiple sidebars without any trouble, I
 could've rewritten the widgets from scratch, but that seemed a waste of
 time. (Note: The update filter is from a patch i just submited: #7233)

 Its not the best solution, But it sure beats having checkbox's on every
 widget :)

 So for the UI, What i thought of was each sidebar having another drop down
 below it, Selecting the "View" that the widgets will belong to, eg,
 "Posts", "Post", "Homepage", etc..

Ticket URL: <http://trac.wordpress.org/ticket/4280#comment:39>
WordPress Trac <http://trac.wordpress.org/>
WordPress blogging software

More information about the wp-trac mailing list