06 December, 2008

Creating widget ready Wordpress themes

I thought writing some thing about creating Wordpress themes.

This post is about creating widget ready Wordpress themes. Is really easy.

Now all you need is two php files in your theme folder
  1. sidebar.php
  2. functions.php
In the sidebar.php just add the following code.

<?php
if(function_exists('dynamic_sidebar'))
dynamic_sidebar();
?>

Then in the functions.php you have to add the following code.

<?php
if ( function_exists('register_sidebar') )
register_sidebar(array(
'before_widget' => '<div id="%1$s" class="sidebar-box %2$s">',
'after_widget' => '</div>',
'before_title' => '<h2 class="widgettitle">',
'after_title' => '</h2>',
));
?>

Now let me explain what we did with these codes,

in the sidebar.php we are calling the dynamic_sidebar function to render the sidebar which is dynamically created by the wordpress, and in the functions.php we are registering a side bar.

Now go to your admin area and then add any widgets to your side bar, when you save your settings you are done. Just refresh the page and see whether it is working.

For further reference read the following page from the Wordpress codex

http://codex.wordpress.org/WordPress_Widgets_Api/dynamic_sidebar

No comments: