Sidebars and Widgets in WordPress

WordPress Templates

A sidebar is an area of a theme that can contain widgets and menus. An administrator can place and customize widgets and menus in the sidebar via the dashboard.

Display the assigned contents of a sidebar in a template file with dynamic_sidebar().

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 with the dynamic_sidebar() function from within a template file.

<?php dynamic_sidebar('main_sidebar'); ?>