register_sidebar()WP 2.2.0

Registers a widget panel (a place where widgets are placed in the admin panel to later display them on the front end).

The function registers a panel (place, container) for widgets and the output format for each widget:

  • html header tag.
  • html tag before and after the widget.

After registering the panel, a place will appear in the admin panel where widgets can be placed. By placing widgets in the panel, we will be able to output the panel with widgets in the template using the function dynamic_sidebar(), (see example 2).

The function needs to be hooked to the action widgets_init.

This function automatically activates widget support. See add_theme_support('widgets')

Do not use identifier names for the ID parameter from this list. There, at the link, you will find a small function that collects already used IDs in WordPress.

Use register_sidebars() to create multiple panels at once.

The default values for the before/after parameters are designed for themes that create sidebars in the form of an LI list with headers in the H2 tag. Such registration is recommended by WordPress developers for all themes.

If for some reason, the markup needs to be changed, it is recommended to copy the id (id="%1$s") and class (class="widget %2$s") attributes as specified by default and not to hardcode them. This is necessary so that they are created dynamically using the sprintf() function.

Do not leave the name and id parameters empty!

They can either not be specified at all, or must be specified, and they need to be unique.

If id or name is not defined, default values with an increment will be used:

'name' => sprintf(__('Sidebar %d'), $i ),
'id' => "sidebar-$i",

Where $i will be an integer equal to the number of panels on the site + 1.

Returns

String. The identifier passed in the ID parameter.

Usage Template

add_action( 'widgets_init', 'register_my_widgets' );
function register_my_widgets(){

	register_sidebar( array(
		'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' => '', // WP 5.6
		'after_sidebar'  => '', // WP 5.6
	) );
}

Usage

register_sidebar( $args );
$args(string/array)

Arguments for registering the widget panel. Can be specified as a string or an array:
If as a string, then the arguments should be separated by the & sign:

'name=Sidebar&id=my_prefix_sidebar'

The same arguments in an array:

array(
	'name' => 'Sidebar',
	'id'   => 'my_prefix_sidebar',
)

Default: [] (pre-settings)

Arguments for the $args parameter

Most of the options below specify suitable for the theme tags wrapping each widget and its title.

name(string)

The name of the widget panel. The name will be visible in the WordPress admin panel. By default "Sidebar 1" (localization from Sidebar $i, where $i is the ordinal number of the sidebar). The value must not be empty!

Although the name will only be visible in the admin panel, it is important to give the user an idea of where the panel will be displayed.

Default: sprintf( __('Sidebar %d'), $i )

id(string)
The identifier of the widget. A string that must not contain uppercase letters or spaces. The value must not be empty; if this parameter is left empty, you will receive a notice like E_USER_NOTICE in development mode (during debugging).
Default: "sidebar-$i"
description(string)
Text describing where the widget panel will be displayed. Shown in the widget management panel.
Default: ''
class(string)
CSS class that will be added to the main HTML tag of the widget panel.
Default: ''
before_widget(string)
HTML code that will be placed before each widget in the panel. For example: <li class="my-widget">. The constructs %1$s and %2$s will be replaced with the id and class used in the sidebar widget.
Default: '<li id="%1$s" class="widget %2$s">'
after_widget(string)
HTML code that will be placed after each widget in the panel. For example: </li>.
Default: "</li>\n"
before_title(string)
HTML code before the widget title.
Default: '<h2 class="widgettitle">'
after_title(string)
HTML code after the widget title.
Default: "</h2>\n"
before_sidebar(string) (WP 5.6)
HTML before the sidebar. Receives the argument $id in %1$s and $class in %2$s. Outputted after the hook dynamic_sidebar_before.
Default: ''
after_sidebar(string) (WP 5.6)
HTML after the sidebar. Outputted before the hook dynamic_sidebar_after.
Default: ''
$show_in_rest(true/false) (WP 5.9)
Should this sidebar be publicly displayed in the REST API.
Default: false (show only to administrators).

Examples

0

#1 Register widgets for the sidebar

The code below, will register a panel called "Right sidebar of the site". The panel name will be wrapped in <div class="title"> and </div> tags:

id parameter value must be in lowercase and no spaces!

add_action( 'widgets_init', 'register_my_widgets' );

function register_my_widgets(){

	register_sidebar( [
		'name' => 'The right sidebar of the site',
		'id' => 'right-sidebar',
		'description' => 'These widgets will be shown in the right column of the site',
		'before_title' => '<h2>',
		'after_title' => '</h2>'
	] );
}

After put this code into the theme functions.php file, a panel will appear in the admin-panel on the widgets page, where we can add widgets. In the template file, this panel is displayed with the function dynamic_sidebar():

<?php
if ( function_exists( 'dynamic_sidebar' ) ) {
	dynamic_sidebar( 'right-sidebar' );
}
?>
0

#2 Register a widget panel for the home page

Add the code to functions.php:

add_action( 'widgets_init', 'register_my_widgets' );

function register_my_widgets(){

	register_sidebar( array(
		'name' => 'Sidebar on home page',
		'id'   => 'homepage-sidebar',
		'description'   => 'Displayed as a sidebar only on the home page of the site.',
		'before_widget' => '<li id="%1$s" class="homepage-widget-block %2$s">',
		'after_widget'  => '</li>',
		'before_title'  => '<h2 class="widgettitle">',
		'after_title'   => '</h2>',
	) );
}

In the file home.php let's insert the panel output:

<div class="custom">
	<?php
	if ( function_exists('dynamic_sidebar') )
		dynamic_sidebar('homepage-sidebar');
	?>
</div>
0

#3 Add Multiple sidebar

There is a special function to do this: see register_sidebars().

Notes

  • Global. Array. $wp_registered_sidebars The registered sidebars.

Changelog

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

register_sidebar() code WP 6.9

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'];
}