Registring widget area and how to use it later

To register a widget area for you theme you should  use the register_sidebar function in your theme’s function.php.

 * Register a sidebars and widget area.
function your_new_widget_area() {

	register_sidebar( array(
		'name'          => __('Name of the Widget Area','text-domain'),
		'id'            => 'id_name',
                'description'   => 'Some description',
                'class'         => 'the-class',
                'before_widget' => '<div>',
		'after_widget'  => '</div>',
		'before_title'  => '<h2 class="widget-title">',
		'after_title'   => '</h2>',
	) );
add_action( 'widgets_init', 'your_new_widget_area' ); 

This function will register the widget’s area through the ‘widgets_init’ hook.

For more specification on register_sidebar please see here.

Next step is to set the place where we want to see the widgets to be displayed. Put the following code in your html code where you want to have to widget’s block to appear.

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

Now you can go to your admin panel Appearance – >Widgets and navigate through your new widget area.



Leave a Reply

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