[wp-hackers] Sidebar ID Naming Problems

Charles K. Clarkson cclarkson at htcomp.net
Mon Nov 16 22:19:38 UTC 2009


scribu wrote:
> I think the first thing you should do is make it so that register_sidebar()
> doesn't allow sidebars that display_sidebar() can't find.
> 
> Or, if it's worth it, make display_sidebar() find all sidebars.

(Its dynamic_sidebar(), not display_sidebar().)

I agree with you, but I want to keep it backward compatible. I think a new
function that standardizes the possible names and patches to each current
sidebar function that needs to find a sidebar, would accomplish a uniform
naming convention which is backward compatible and allows the use of named
arguments.

I started this, but haven't fully debugged it yet. I stopped to add this
thread to the list. Much of it is just a copy of present code which we
know works. Its the mainly named arguments which need to be debugged.

/**
  * Return the id of a sidebar given its name or the integer portion of
  * 'sidebar-1' styled ids.
  *
  * Since register_sidebar() supports it, this function will look for sidebars
  * named as integers or as a default id (like 2 or 'sidebar-13') first and check
  * those as ids if the name fails (and those ids exist).
  *
  * @since 2.9.0
  *
  * @param int|string|array $args The name or id of the wigdet specified by
  * register_sidebar(s) functions. As an array use the register_sidebar() syntax
  * identifying sidebar by name or id or both.
  *
  * @return mixed returns false if id not found or returns sidebar id.
  */
function get_sidebar_id ($args) {
     global $wp_registered_sidebars;

     if ( ! is_array($args) ) {

         // $args is a name or an id
         $name_or_id = $args;

         // for backward compatibility
         if ( is_int($name_or_id) ) {
             return "sidebar-$name_or_id";

         } else {
             $name_or_id = sanitize_title($name_or_id);
             foreach ( (array) $wp_registered_sidebars as $id => $value ) {
                 if ( sanitize_title($value['name']) == $name_or_id ) {
                     return $id;
                 }
             }
         }

         return $name_or_id;

     } else {

         if ( !array_key_exists('id', $args) and !array_key_exists('name', $args) )
             return false;

         // $args is an array holding a name, an id or both.
         if ( array_key_exists('id', $args) && array_key_exists($args['id'], 
$wp_registered_sidebars) )
             return $args['id'];

         if ( array_key_exists('name', $args) ) {
             $name = sanitize_title($args['name']);
             foreach ( (array) $wp_registered_sidebars as $id => $value ) {
                 if ( sanitize_title($value['name']) == $name ) {
                     return $id;
                 }
             }
         }
     }

     return false
}



HTH,

Charles Clarkson
-- 
Mobile Home Investor
Free Market Advocate
Programmer

I'm not really a smart person. I just play one on the Internet.

Stephenville, TX
http://www.clarksonenergyhomes.com/wordpress/about/
http://twitter.com/CharlesClarkson
+1 (254) 968-8328


More information about the wp-hackers mailing list