Saturday, July 30, 2011

Adding Widget area in a wordpress theme

Its very easy and a few steps required to add a widget area to your Wordpress theme.

Step 1: Register your widget
In your functions.php add register_sidebar() function.



<?php
if ( function_exists('register_sidebar') )
register_sidebar(array(
'id'            => 'right-sidebar',
'name' => 'RightSideBar',
'description' => 'Widgets in this area will be shown on the right-hand side.',
'before_widget' => '',
'after_widget' => '',
'before_title' => '<h2>',
'after_title' => '</h2>',
));
?>



Parameters:
It is pretty self-explanatory.
  • name - Sidebar name.
  • id - Sidebar id - Must be all in lowercase, with no spaces.
  • description - Text description of what/where the sidebar is. Shown on widget management screen. (Since 2.9)
  • before_widget - HTML to place before every widget.
  • after_widget - HTML to place after every widget.
  • before_title - HTML to place before every title.
  • after_title - HTML to place after every title.
Step 2: displaying widget at the right position
<?php
 if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('right-sidebar') ) : 
endif; ?>
Parameters:
  • only (optional) argument to the function:Name or ID of dynamic sidebar.