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; ?>