Create a custom widget area in WordPress theme

In these tutorials, we study how to create a custom widget area in the WordPress theme.

In WordPress, Every theme comes with a widget section area. Every widget is placed in the sidebar. Widgets areas are used to add posts, footer, archives, recent comments, search, category, links, navigation menus, links, calendar, pages, and etc.

Create a custom widget area

In your WordPress theme folder, the function.php file is present. If your WordPress has not this file, then you need to do create a new file and then name it as “function.php”.

Then write the code and then insert it in function.php file:

if ( function_exists('register_sidebar') ) {
register_sidebar( array(
'name' => 'Home Main Content Area',
'id' => 'Home Main Content Area',
'description' => new widget area,
'before_widget' => '<div class="Home Main Content Area">',
'after_widget' => '</div>',
'before_title' => '<h4 class="Home Main Content Area">',
'after_title' => '</h4>',
) );

The above code will create a new widget named “Home Main Content Area”. The code above should be enclosed in <?PHP and ?> PHP tags .

The ‘before_title’ and ‘after_title’  is <h4> and </h4> tags which is allow to bound the code in tags.

The ‘before_widget’ and ‘after_widget’ is div in this above code which allows code you put before & after the widget.

Now we write code, we used this code for using the ‘widgets’ so we insert that code in the area we want.

<?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar("Home Main Content Area") ) : ?>
<?php endif;?>

Now, everything widget we do in “Home Main Content Area”, it will be seen on the homepage in the WordPress dashboard.

I hope this tutorial will help you.

Leave a Reply

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