Create Custom Widget Area in WordPress theme

A WordPress Widget is a small block that performs a specific function. Normally every widget placed in a widgetized area called sidebar. Every theme comes with widget area section.

But its very helpful for you to learn how to create a custom widget area. So you can create your own widget area section anywhere you want or according to your client’s need.

Let’s start and create a custom widget area.

1. Registering a custom widget area

To registering a widget area add following code in your theme’s functions.php file.

function smallenvelop_widgets_init() {
    register_sidebar( array(
        'name' => __( 'Header Sidebar', 'smallenvelop' ),
        'id' => 'header-sidebar',
        'before_widget' => '<div>',
        'after_widget' => '</div>',
        'before_title' => '<h1>',
        'after_title' => '</h1>',
    ) );
}
add_action( 'widgets_init', 'smallenvelop_widgets_init' );

 2. Display Widget Area

To display Widget Area add the following code to a location of your choice in your theme file. Here I am adding this code in my theme’s header.php

<?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('header-sidebar') ) : 
 
endif; ?>

All done, this is how we create new widget area!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Do NOT follow this link or you will be banned from the site!
%d bloggers like this: