August 12th, 2011

Posted in Reference + Wordpress

Multiple Widget Areas in WordPress

As I develop more and more themes,  the benefits of using widgets are obviously apparent, not just when developing WordPress themes for a ‘pure’ blog but as an easy way for clients to update websites when I have created more of a CMS theme for them. Plus many useful plugins come only in widget form, rather than having the ability to insert code into templates.

The problem I came up against is many themes I have picked apart and analysed came with only one widget area. In reality you often need two or more widget enabled areas. Here for my future reference and yours is some code snippets that will easily allow you to have as many widget areas as you want or need.

The first file we want to look at is functions.php We want to register these widget enabled areas so they appear in the WordPress administration area. All we need to do is add the following code to our functions.php file

if ( function_exists(‘register_sidebar’) ) {
register_sidebar(array(‘name’=>’widget area’,
‘before_widget’ => ‘<li id=”%1$s”>’,
‘after_widget’ => ‘</li>’,
‘before_title’ => ‘<h2>’,
‘after_title’ => ‘</h2>’,
));
}

Where we have: register_sidebar(array(‘name’=>’widget area’,
‘widget area’ is the name of our new widget. It could be ‘footer widget area’, it could be ‘banner adverts’ whatever is appropriate.

You will see some html there as well to wrap our widget in, pretty straight forward.

Step two is as simple as opening up the template that you want this new widget area to appear on, locating the div you would  like it to appear and then pasting this code:

<?php if ( !function_exists(‘dynamic_sidebar’) || !dynamic_sidebar(‘widget area’) ) : ?>
<?php endif; ?>

Again nice and straight forward, just replace ‘widget area’ with the name of the widget area you created. Log into your WordPress dashboard and navigate to the widget area and customise away. After that it is just a case of rinse and repeat, add as many individually named widget areas as you like.

Leave a Reply