Create a Custom Widget Area

Published on : June 3, 2014

Author:

Category: Wordpress


Widget is most popular technic in wordpress. But if we need extra widgets in wordpress then custom code is needed. Here is a small code. Following the example you can use it in your site to make custom widgets.

Add this code in your function.php . this code register your widget Area into the theme.

Code:



add_action( 'widgets_init', 'widgets_init' );

function widgets_init() {
   register_sidebar( array(
   'name' => __( 'Top Sidebar Area', 'Site Name' ),
   'id' => 'top-slidebar',
   'description' => __( 'The Top Sidebar Area Put Your Widgets Here', 'yoursite' ),
   'before_widget' => '',
   'after_widget' => '',
   'before_title' => '<h1 class=”widget-title”>',
   'after_title' => '</h1>',
));

/*
* THE REST OF THE SIDEBAR REGISTRATION CODE FROM
*/

}

‘name’

Name of the Widget area show on widget page.

‘id’

It mainly depend to print the widget. It is the widget unique identifier.

‘description’

Description show below of  the Widget title.

‘before_widget’

It is widget content container. You can add here a <div>. wrapper start from here.

‘after_widget’

It is widget content container. You can add here a </div>. wrapper close from here.

‘before_title’

Before the heading title add this code .

‘after_title’

After  the heading title add this code .

Print the widget area:


<?php if ( is_active_sidebar('top-slidebar') ) : ?>
   <?php dynamic_sidebar('top-slidebar'); ?>
<?php endif; ?>

If the Top widget is active then print it. You can put this code in header.php , footer.php or other theme file where you need it.