[wp-trac] [WordPress Trac] #11160: Inconsistancies in Naming and Using Sidebar Names and IDs.

WordPress Trac wp-trac at lists.automattic.com
Tue Nov 17 12:58:57 UTC 2009


#11160: Inconsistancies in Naming and Using Sidebar Names and IDs.
-----------------------------+----------------------------------------------
 Reporter:  CharlesClarkson  |       Owner:  azaozz                     
     Type:  defect (bug)     |      Status:  new                        
 Priority:  normal           |   Milestone:  2.9                        
Component:  Widgets          |     Version:  2.9                        
 Severity:  normal           |    Keywords:  sidebar, sidebars, id, name
-----------------------------+----------------------------------------------
 register_sidebar() allows more sidebar names and IDs to be registered than
 dynamic_sidebar() recognizes as valid names and IDs.

 For example, register_sidebar() allows me to name a side bar "1" with a id
 of "first". I don't know why anyone would choose those values, but
 register_sidebar() allows it [1].

 {{{ register_sidebar( array('name' => 1, id => 'first') ); }}}


 dynamic_sidebar() will not be able to find the sidebar given its name (1).
 {{{
     if ( is_int($index) ) {
         $index = "sidebar-$index"; /// 1 becomes 'sidebar-1'
         ...
 }}}
 The main problem is that dynamic_sidebar() is trying to process both IDs
 and names through the same variable ($index) while register_sidebar()
 separates the two with an array ( array('name' => 'Top', 'id' =>
 'sidebar-1' ).

 According to the in-line docs for dynamic_sidebar():

     It is confusing for the $index parameter, but just know that it should
 just work. When you register the sidebar in the theme, you will use the
 same name for this function or "Pay no heed to the man behind the
 curtain." Just accept it as an oddity of WordPress sidebar register and
 display.


 It does "just work" if you never use your own sidebar IDs.


 I started looking at this because I wanted to use is_active_sidebar()
 which tests to see if a dynamic_sidebar() has anything in it. There is no
 get_dynamic_sidebar(). dynamic_sidebar() sends everything to the browser
 or returns false.
 {{{
     register_sidebar( array('name' => 'Top') ); // id defaults to
 "sidebar-1"
     ...

     if ( is_active_sidebar('Top') )
         dynamic_sidebar('Top');
 }}}
 Which fails because is_active_sidebar() just completely skips over
 searching for an id to go with a name. To get it to work you need to know
 when it was registered. Not something theme authors and designers are
 going to follow easily. There's a ticket to fix this:
 [http://core.trac.wordpress.org/ticket/10440 #10440]
 {{{
     if ( is_active_sidebar(1) )
         dynamic_sidebar('Top');
 }}}
 Like dynamic_sidebar(), is_active_sidebar() converts 1 to "sidebar-1".
 Unlike dynamic_sidebar() it assumes everything is entered as an id.


 unregister_sidebar() assumes its parameter (incorrectly named $name, not
 $id) is an id. But it wants a literal id, like "sidebar-1".
 unregister_sidebar(1) unregisters a sidebar with an id of 1, while
 dynamic_sidebar(1) tries to display a sidebar with an id of "sidebar-1".


 === Widgets (Admin Page) ===

 The dynamic_sidebar() function is used by the Widgets management page. So,
 it is possible to create a sidebar with register_sidebar() that
 dynamic_sidebar() cannot find. You can populate it with drag and drop [2]
 and not have it appear on the web site.


 == After Patch ==

 If committed, this patch would remove the need for tickets
 [http://core.trac.wordpress.org/ticket/10440 #10440] and
 [http://core.trac.wordpress.org/ticket/10956 #10956]. It changes the
 current argument behavior of unregister_sidebar(), but doesn't break
 backward compatibility. It allows is_active_sidebar(),
 unregister_sidebar() and dynamic_sidebar() all point to the same sidebar.

 === Before ===

 These all refer to the same sidebar:
 {{{
         is_active_sidebar(1);
         unregister_sidebar('sidebar-1');
         dynamic_sidebar('Sidebar Top');
 }}}

 In an admittedly contrived case, dynamic_sidebar() would silently fail to
 allow this sidebar to show:
 {{{ register_sidebar( array('name'=>'Sidebar Top', 'id' => 1) ); }}}



 === After ===
 These all refer to the same sidebar (the first two would have broken
 before the patch):
 {{{
         is_active_sidebar('Sidebar Top');
         unregister_sidebar('Sidebar Top');
         dynamic_sidebar('Sidebar Top');
 }}}

 After the patch this shows fine:

 {{{ register_sidebar( array('name'=>'Sidebar Top', 'id' => 1) ); }}}

 After the patch it is possible to force an argument to be only a name or
 only an id:
 {{{
         is_active_sidebar(array( 'name' => 'Sidebar Top' ));
         unregister_sidebar(array( 'name' => 'Sidebar Top' ));
         dynamic_sidebar(array( 'id' => 1 ));
 }}}



 === Notes ===

 [1] register_sidebar() allows the user to override the default setting of:
 'id' => "sidebar-$i",

 [2] When you refresh the Widgets management page the widgets will
 disappear from the sidebar. They are still attached to a sidebar, but
 dynamic_sidebar() cannot see the sidebar.

-- 
Ticket URL: <http://core.trac.wordpress.org/ticket/11160>
WordPress Trac <http://core.trac.wordpress.org/>
WordPress blogging software


More information about the wp-trac mailing list