WordPress Sidebars
In WordPress a sidebar (also known as a widget area or a widget-ready area) is any region of the page that can contain widgets and menus. The Sidebar theme feature allows admin users to assign widgets to Sidebar defined by a theme.
There is no default sidebar in a WP theme. The number of sidebars and where they are located is defined by the theme. Most WordPress themes offer at least one sidebar for a site’s "left sidebar".
An administrator can place and customize widgets (blocks of content) and menus in a sidebar via the dashboard. WordPress widgets were created to provide an easy way for WordPress admin users to control the design and content of their site without having to code. Most WordPress themes support widgets.
WP sidebars can hold admin-created navigation menus (although menu locations cannot hold widgets).
Useing Widgets and Sidebars
To use widgets and sidebars in a theme:
- Register the Sidebars Theme Feature in the
functions.php
file. - Register each widget area using
register_sidebar()
. - Display each widget area using
dynamic_sidebar()
in template files.
Register a Sidebar
Register a sidebar with the register_sidebar() function and the widgets_init
action hook from within the functions.php
theme file.
add_action( 'widgets_init', 'my_register_sidebars' );
function my_register_sidebars(){
register_sidebar(
array(
'id' => 'main_sidebar',
'name' => __( 'Main Sidebar' ),
'description' => __( 'Widgets in this area will be shown on all posts and pages.' ),
'before_widget' => '<aside id="%1$s" class="widget %2$s">',
'after_widget' => '</aside>',
'before_title' => '<h3>',
'after_title' => '</h3>'
)
);
/* Repeat register_sidebar() code here for each additional sidebar. */
}
Display a Sidebar
Display a sidebar in a template file with the dynamic_sidebar() function from within a template file.
<?php dynamic_sidebar('main_sidebar'); ?>
More Resources
- https://developer.wordpress.org/themes/functionality/sidebars/
- https://codex.wordpress.org/Sidebars
- https://developer.wordpress.org/reference/functions/register_sidebar/
- https://developer.wordpress.org/reference/functions/dynamic_sidebar/
- https://developer.wordpress.org/reference/functions/get_sidebar/
- https://wordpress.org/support/article/appearance-widgets-screen/