How To Add Custom Widget Areas to WordPress Themes
If you are looking to add WordPress custom widget area to your WordPress, then you have come to the right place. This article will take you through the entire process of adding a custom widget through the custom widget WordPress code.
Table of Content
But before we skip to that part, let’s see why do you need a WordPress custom widget.
Why do You Need a WordPress Custom Widget
Widget areas allow you to add content on different areas on your website like the header or the sidebar. These areas can be used to display ads, featured content or recent articles of your website. Basically, these areas are for any content that you want your audience to check out.
Most WordPress themes come with widget areas like headers or sidebars. But if the case is otherwise, then this article will help you add a WordPress custom widget to your website.
How to Add Custom Widget in WordPress
Now it’s timeto 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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
function wpblog_widget() { register_sidebar(array( '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>', )); } add_action('widgets_init', 'wpblog_widget'); |
Make WordPress Custom Widget Area Visible
1 2 3 4 5 |
<?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.
Conclusion
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.
Create Faster WordPress Websites!
Free eBook on WordPress Performance right in your inbox.