Widgets are such an essential aspect of WordPress that it is hard to find a theme that doesn’t support widget areas. In practical terms, widgets are executable scripts that users could drag-and-drop in sidebars or other designated widget areas.
In this article, I will demonstrate how to add custom widget-ready areas (including sidebars) to your WordPress theme.
Add Register sidebar
To register a widget, add the following code to the functions.php, located in the theme folder:
'name' => __('Primary Sidebar', 'wpb'),
'id' => 'primary_sidebar', // unique-sidebar-id
'description' => '',
'class' => '',
'before_widget' => '<li id="%1$s" class="widget %2$s">',
'after_widget' => '</li>',
'before_title' => '<h2 class="widgettitle">',
'after_title' => '</h2>',
Make Widget Area Visible
<?php if ( is_active_sidebar( 'Primary Sidebar' ) ) : ?>
<?php dynamic_sidebar( 'Primary Sidebar' ); ?>
<?php endif; ?>
Before the users could add widgets to the newly created sidebars, you need to add to add the following code to a custom template such as sidebar.php (or any other conveniently named file):
Once this is done, users could drag-and-drop widgets to the newly created areas.
Adding custom widget areas is an important step in extending the use of widgets in the WordPress theme. Just follow the steps and you are good to go. If you need help, do leave a comment and I will get back to you.