register_sidebars

Creates multiple sidebars.

Description

register_sidebars( (int) $number = 1, (array) $args = array() ); 

If you wanted to quickly create multiple sidebars for a theme or internally. This function will allow you to do so. If you don't pass the name and/or 'id' in $args,, then they will be built for you.

Parameters (2)

0. $number — Optional. (int) => 1
Number of sidebars to create. Default 1.
1. $args — Optional. (array) => array()
Array or string of arguments for building a sidebar.

Options

  • id (string) => 'sidebar-'

    The base string of the unique identifier for each sidebar. If provided, and multiple sidebars are being defined, the id will have -2 appended, and so on.

array(

    /**
     * The base string of the unique identifier for each sidebar. If provided, and multiple sidebars
     * are being defined, the id will have "-2" appended, and so on.
     *
     * @type string
     * @default 'sidebar-'
     */
    'id' => 'sidebar-'
);        


Usage

  1. if ( !function_exists( 'register_sidebars' ) ) { 
  2. require_once ABSPATH . WPINC . '/widgets.php'; 
  3.  
  4. // Optional. Number of sidebars to create. Default 1. 
  5. $number = 1; 
  6.  
  7. // Optional. Array or string of arguments for building a sidebar. 
  8. $args = array( 
  9. 'id' => 'sidebar-' 
  10. ); 
  11.  
  12. // NOTICE! Understand what this does before running. 
  13. $result = register_sidebars($number, $args); 
  14.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/widgets.php  
  1. function register_sidebars( $number = 1, $args = array() ) { 
  2. global $wp_registered_sidebars; 
  3. $number = (int) $number; 
  4.  
  5. if ( is_string($args) ) 
  6. parse_str($args, $args); 
  7.  
  8. for ( $i = 1; $i <= $number; $i++ ) { 
  9. $_args = $args; 
  10.  
  11. if ( $number > 1 ) 
  12. $_args['name'] = isset($args['name']) ? sprintf($args['name'], $i) : sprintf(__('Sidebar %d'), $i); 
  13. else 
  14. $_args['name'] = isset($args['name']) ? $args['name'] : __('Sidebar'); 
  15.  
  16. // Custom specified ID's are suffixed if they exist already. 
  17. // Automatically generated sidebar names need to be suffixed regardless starting at -0 
  18. if ( isset($args['id']) ) { 
  19. $_args['id'] = $args['id']; 
  20. $n = 2; // Start at -2 for conflicting custom ID's 
  21. while ( is_registered_sidebar( $_args['id'] ) ) { 
  22. $_args['id'] = $args['id'] . '-' . $n++; 
  23. } else { 
  24. $n = count( $wp_registered_sidebars ); 
  25. do { 
  26. $_args['id'] = 'sidebar-' . ++$n; 
  27. } while ( is_registered_sidebar( $_args['id'] ) ); 
  28. register_sidebar($_args);