How To Add Custom Widget Areas to WordPress Themes
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.
Subscribe to Get The A FREE WordPress Ebook Right in Your Inbox
WPblog provides the complete guide to launch your WordPress website completely FREE!
Moeez is ‘The’ blogger in charge of WPblog. He loves to interact and learn about WordPress with people in the WordPress community. Outside his work life, Moeez spends time hanging out with his friends, playing Xbox and watching football on the weekends. You can get in touch with him at moeez[at]wpblog.com.