register_sidebar( array|string $args = array() ): string
- Since
- 2.2.0, 5.6.0, 5.9.0
- Source
wp-includes/widgets.php:269
Description
Accepts either a string or an array and then parses that against a set of default arguments for the new sidebar. WordPress will automatically generate a sidebar ID and name based on the current number of registered sidebars if those arguments are not included.
When allowing for automatic generation of the name and ID parameters, keep in mind that the incrementor for your sidebar can change over time depending on what other plugins and themes are installed.
If theme support for 'widgets' has not yet been added when this function is called, it will be automatically enabled through the use of add_theme_support().
Compatibility
- WordPress
- since 5.9.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
$argsarray|stringoptional- Array or string of arguments for the sidebar being registered.Default:
array()$namestringdefault: 'Sidebar $instance'The name or title of the sidebar displayed in the Widgets interface.$idstringdefault: 'sidebar-$instance'The unique identifier by which the sidebar will be called.$descriptionstringdefault: empty stringDescription of the sidebar, displayed in the Widgets interface.$classstringdefault: emptyExtra CSS class to assign to the sidebar in the Widgets interface.$before_widgetstringdefault: is an opening list item elementHTML content to prepend to each widget's HTML output when assigned to this sidebar. Receives the widget's ID attribute as%1$sand class name as%2$s.$after_widgetstringdefault: is a closing list item elementHTML content to append to each widget's HTML output when assigned to this sidebar.$before_titlestringdefault: is an opening h2 elementHTML content to prepend to the sidebar title when displayed.$after_titlestringdefault: is a closing h2 elementHTML content to append to the sidebar title when displayed.$before_sidebarstringdefault: empty stringHTML content to prepend to the sidebar when displayed. Receives the$idargument as%1$sand$classas%2$s. Outputs after the 'dynamic_sidebar_before' action.$after_sidebarstringdefault: empty stringHTML content to append to the sidebar when displayed. Outputs before the 'dynamic_sidebar_after' action.$show_in_restboolWhether to show this sidebar publicly in the REST API. Defaults to only showing the sidebar to administrator users.
Return value
string- Sidebar ID added to $wp_registered_sidebars global.
Performance profile
How much work a call to register_sidebar() 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
- Trivial
- Scaling
- Constant
- Instructions
- 47–63
- Plugin surface
- 2 hooks
- Called by
- 12
Touches nothing outside its own arguments.
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 63.
Third-party callbacks on 'register_sidebar_defaults', 'register_sidebar' run inside this call, and their cost is not bounded by anything here.
12 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_filters()called directly
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_sidebar() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
!empty($args) | 47 | __(), sprintf(), apply_filters(), wp_parse_args(), add_theme_support(), do_action() |
empty($args) | 63 | __(), sprintf(), apply_filters(), wp_parse_args(), __(), sprintf(), _doing_it_wrong(), add_theme_support(), do_action() |
Across PHP versions
Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 63 instructions, 47–63 executed per call, 1 branch. The work does not change between versions.
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.
Hooks and filters fired · 2
2 hooks fire while register_sidebar() runs, in this order:
- apply_filters( register_sidebar_defaults )filterline 300 (+31 into the body)
Filters the sidebar default arguments.
- do_action( register_sidebar )actionline 327 (+58 into the body)
Fires once a sidebar has been registered.
Uses · 6
- __()Retrieves the translation of $text.
- wp_parse_args()Merges user defined arguments into defaults array.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- _doing_it_wrong()Marks something as being incorrectly called.
- add_theme_support()Registers theme support for a given feature.
- do_action()Calls the callback functions that have been added to an action hook.
Used by · 12
- register_sidebars()Creates multiple sidebars.
- twenty_twenty_one_widgets_init()Registers widget area.
- twentyeleven_widgets_init()Registers sidebars and widgetized areas.
- twentyfifteen_widgets_init()Registers widget area.
- twentyfourteen_widgets_init()Registers three Twenty Fourteen widget areas.
- twentynineteen_widgets_init()Registers widget area.
- twentyseventeen_widgets_init()Registers widget area.
- twentysixteen_widgets_init()Registers a widget area.
- twentyten_widgets_init()Registers widgetized areas, including two sidebars and four widget-ready columns in the footer.
- twentythirteen_widgets_init()Registers two widget areas.
- twentytwelve_widgets_init()Registers sidebars.
- twentytwenty_sidebar_registration()Registers widget areas.
Source code
function register_sidebar( $args = array() ) { global $wp_registered_sidebars; $i = count( $wp_registered_sidebars ) + 1; $id_is_empty = empty( $args['id'] ); $defaults = array( /* translators: %d: Sidebar number. */ 'name' => sprintf( __( 'Sidebar %d' ), $i ), 'id' => "sidebar-$i", 'description' => '', 'class' => '', 'before_widget' => '<li id="%1$s" class="widget %2$s">', 'after_widget' => "</li>\n", 'before_title' => '<h2 class="widgettitle">', 'after_title' => "</h2>\n", 'before_sidebar' => '', 'after_sidebar' => '', 'show_in_rest' => false, ); /** * Filters the sidebar default arguments. * * @since 5.3.0 * * @see register_sidebar() * * @param array $defaults The default sidebar arguments. */ $sidebar = wp_parse_args( $args, apply_filters( 'register_sidebar_defaults', $defaults ) ); if ( $id_is_empty ) { _doing_it_wrong( __FUNCTION__, sprintf( /* translators: 1: The 'id' argument, 2: Sidebar name, 3: Recommended 'id' value. */ __( 'No %1$s was set in the arguments array for the "%2$s" sidebar. Defaulting to "%3$s". Manually set the %1$s to "%3$s" to silence this notice and keep existing sidebar content.' ), '<code>id</code>', $sidebar['name'], $sidebar['id'] ), '4.2.0' ); } $wp_registered_sidebars[ $sidebar['id'] ] = $sidebar; add_theme_support( 'widgets' ); /** * Fires once a sidebar has been registered. * * @since 3.0.0 * * @param array $sidebar Parsed arguments for the registered sidebar. */ do_action( 'register_sidebar', $sidebar ); return $sidebar['id'];}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.
show_in_rest argument.from the docblockbefore_sidebar and after_sidebar arguments.from the docblockAbout 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.