wordpress creating custom widget area

Posted on December 30, 2015 by Suresh Kamrushi in PHP

Today i have created a custom widget area in my theme. Thought of sharing it.
Follow below two easy steps to enable it:
1) Add below lines of code to your theme functions.php file

 * Register our sidebars and widgetized areas.
function arphabet_widgets_init() {

     register_sidebar( array(
       'name' => 'Social Networking sidebar',
       'id' => 'Social-Networking-sidebar',
       'description' => __( 'Social networking info', 'kwikkar' ),
       'before_widget' => '<div>',
       'after_widget' => '</div>',
       'before_title' => '<h2 class="rounded">',
      'after_title' => '</h2>',
 ) ); 

add_action( 'widgets_init', 'arphabet_widgets_init' );

Or you can add function register_sidebar() if ‘widgets_init’ action is already added in your theme function.php file.
Now if you go to admin -> appearance -> widgets “Social Networking sidebar” is start appearing on your page.

2) Now, next is to show widget in new area. Add below code at desire location, such as sidebar.php, header.php or footer.php

<?php if ( is_active_sidebar( 'home_right_1' ) ) : ?>
	<div id="primary-sidebar" class="primary-sidebar widget-area" role="complementary">
		<?php dynamic_sidebar( 'home_right_1' ); ?>
	</div><!-- #primary-sidebar -->
<?php endif; ?>

Reff: http://codex.wordpress.org/Widgetizing_Themes

  Comments or questions are welcome  

Tagged as , ,