wppaste
WordPress

register_sidebar( array|string $args = array() ): string

Since
2.2.0, 5.6.0, 5.9.0
Source
wp-includes/widgets.php:269
Builds the definition for a single sidebar and returns the ID.

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 string

    Description of the sidebar, displayed in the Widgets interface.
  • $classstringdefault: empty

    Extra CSS class to assign to the sidebar in the Widgets interface.
  • $before_widgetstringdefault: is an opening list item element

    HTML content to prepend to each widget's HTML output when assigned to this sidebar. Receives the widget's ID attribute as %1$s and class name as %2$s.
  • $after_widgetstringdefault: is a closing list item element

    HTML content to append to each widget's HTML output when assigned to this sidebar.
  • $before_titlestringdefault: is an opening h2 element

    HTML content to prepend to the sidebar title when displayed.
  • $after_titlestringdefault: is a closing h2 element

    HTML content to append to the sidebar title when displayed.
  • $before_sidebarstringdefault: empty string

    HTML content to prepend to the sidebar when displayed. Receives the $id argument as %1$s and $class as %2$s. Outputs after the 'dynamic_sidebar_before' action.
  • $after_sidebarstringdefault: empty string

    HTML content to append to the sidebar when displayed. Outputs before the 'dynamic_sidebar_after' action.
  • $show_in_restbool

    Whether 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

Touches nothing outside its own arguments.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
47–63

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 63.

Plugin surface
2 hooks

Third-party callbacks on 'register_sidebar_defaults', 'register_sidebar' run inside this call, and their cost is not bounded by anything here.

Called by
12

12 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksapply_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.

WhenInstructionsCalls 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:

  1. apply_filters( register_sidebar_defaults )filterline 300 (+31 into the body)

    Filters the sidebar default arguments.

  2. do_action( register_sidebar )actionline 327 (+58 into the body)

    Fires once a sidebar has been registered.

Uses · 6

Used by · 12

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.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

5.9.0
Added the show_in_rest argument.from the docblock
5.6.0
Added the before_sidebar and after_sidebar arguments.from the docblock
2.2.0
Introduced.from the docblock

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.