How to add sidebar in WordPress?

Widgets are such an important part of WordPress themes. It is hard to imagine a WordPress theme without widgets. Widgets were designed to provide a simple and easy-to-use way of giving design and structure control of the WordPress. You can simply drag and drop in your primary & custom sidebar in your theme.

Do you know how to add sidebar in WordPress? This article is for those users who want to learn how to add dynamic sidebar or widget ready areas in WordPress themes.

Add below code in functions.php:

if ( function_exists('register_sidebar') ){
register_sidebar(array(
'id' => 'mysidebar',
'name'=>'My Sidebar Title',
'before_widget' => '', 
'after_widget' => '',
'before_title' => '',
'after_title' => '',
));
}

Now add below code in your .php file wherever you need to add the widget:

<div class="col-md-3">
<?php if (is_active_sidebar('mysidebar') ) { 
    dynamic_sidebar('mysidebar');
}; ?>
</div>

Leave a Reply

Your email address will not be published. Required fields are marked *