How to create a Widget Area in WordPress theme

How to create a Widget Area in WordPress theme

Widget Area is a part of a WordPress theme structure where Widgets can be added.Widget area is normally in right side of main content area or in left side of it.It can be created in header area of website,in footer area of website or in the middle of posts.Almost anywhere you want.
Widget areas also referred as Sidebars.

To add a Widget Go to Appearance > Widgets and Drag a Widget to a Widget Area.Here you can add available Widgets.

Most new themes are widgetized now.check your theme Widgets Section for that.Plugins also create Widgets to display contents or some special features.

So, Let’s start and create a widget area

Creating a Widget Area

There are two steps to create a Widget Area.

1. Registering a Widget Area

Add following code in your theme’s functions.php file.

< ?php /** * Register Widget Area. * */ function wpgyan_widgets_init() { register_sidebar( array( 'name' => ‘Header Sidebar’,
‘id’ => ‘header_sidebar’,
‘before_widget’ => ‘

‘,
‘after_widget’ => ‘

‘,
‘before_title’ => ‘

class=”rounded”‘,
‘after_title’ => ‘

‘,
) );
}
add_action( ‘widgets_init’, ‘wpgyan_widgets_init’ );
?>

Note: I have added prefix wpgyan_ to function name at two places at top and at bottom.You can change to a prefix of your choice.

2. Display Widget Area

To display Widget Area add following code to location of your choice in your theme file.

< ?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('header_sidebar') ) : endif; ?>

Where:
header_sidebar is id of Widget area in Step 1.

To Preview new created Widget Area you can go to Appearance > Widgets.There Must be a Widget area of name “Header Sidebar”.

Multiple Widget areas can be registered using above code.Just use different id for each Widget Area.

Share this post