register_sidebars( int $number = 1, array|string $args = array() )
- Since
- 2.2.0
- Source
wp-includes/widgets.php:174
Description
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.
Compatibility
- WordPress
- since 2.2.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Parameters
$numberintoptional- Number of sidebars to create. Default 1.Default:
1 $argsarray|stringoptional- Array or string of arguments for building a sidebar.Default:
array()$idstringdefault: 'sidebar-' followed by the number the sidebar creation is currently atThe 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.$namestringdefault: 'Sidebar' for the first sidebar, otherwise 'Sidebar %d'The name or title for the sidebars displayed in the admin dashboard. If registering more than one sidebar, include '%d' in the string as a placeholder for the uniquely assigned number for each sidebar.
Performance profile
How much work a call to register_sidebars() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Light
- Scaling
- Scales with input
- Instructions
- 12–16
- Plugin surface
- None
- Called by
- 0
Touches nothing outside its own arguments.
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 82.
Nothing here hands control to plugin code.
Nothing in core calls this; the cost is only what you spend yourself.
What it touches
- hookthird-party callbacks
apply_filters()one call below register_sidebars()
Further down the call graph this can also reach query, option, cache, serialize and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 2 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost register_sidebars() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
!is_string($args) && !$i | 12 | none |
is_string($args) && !$i | 16 | parse_str() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 81 | 12–16 | 8 | 1 fewer instruction than PHP 8.5 |
| 8.5 | 82 | 12–16 | 8 | |
| 8.4 | 82 | 12–16 | 8 | |
| 8.3 | 82 | 12–16 | 8 | |
| 8.2 | 82 | 12–16 | 8 | 1 more instruction than PHP 8.1 |
| 8.1 | 81 | 12–16 | 8 | |
| 7.4 | 81 | 12–16 | 8 |
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Uses · 3
- __()Retrieves the translation of $text.
- is_registered_sidebar()Checks if a sidebar is registered.
- register_sidebar()Builds the definition for a single sidebar and returns the ID.
Source code
function register_sidebars( $number = 1, $args = array() ) { global $wp_registered_sidebars; $number = (int) $number; if ( is_string( $args ) ) { parse_str( $args, $args ); } for ( $i = 1; $i <= $number; $i++ ) { $_args = $args; if ( $number > 1 ) { if ( isset( $args['name'] ) ) { $_args['name'] = sprintf( $args['name'], $i ); } else { /* translators: %d: Sidebar number. */ $_args['name'] = sprintf( __( 'Sidebar %d' ), $i ); } } else { $_args['name'] = $args['name'] ?? __( 'Sidebar' ); } /* * Custom specified ID's are suffixed if they exist already. * Automatically generated sidebar names need to be suffixed regardless starting at -0. */ if ( isset( $args['id'] ) ) { $_args['id'] = $args['id']; $n = 2; // Start at -2 for conflicting custom IDs. while ( is_registered_sidebar( $_args['id'] ) ) { $_args['id'] = $args['id'] . '-' . $n++; } } else { $n = count( $wp_registered_sidebars ); do { $_args['id'] = 'sidebar-' . ++$n; } while ( is_registered_sidebar( $_args['id'] ) ); } register_sidebar( $_args ); }}Changelog
Introduced in 2.2.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 tag, from
src/wp-includes/widgets.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.