Feb
06
2009

How to Widgetize your WordPress sidebar

by   |  Posted in Tutorials  |  25 comments

WordPress widgets are great. They have tons of amazing functions and are extremely easy to use. The only problem is that not all themes support them. But that is a problem that is very easily solved with just a little bit of coding.

First you need to open up your theme’s functions.php file. IF you don’t have one, create one and place it in your theme’s directory. Make sure you have an opening and closing PHP tag.

Here you have two options. The simple option is:

if (function_exists("register_sidebar")) {
register_sidebar();
}

The more complex option is:

if (function_exists("register_sidebar")) {
  register_sidebar(array(
    'name' => 'left-sidebar',
    'before_widget' => '<div class="side-widget">',
    'after_widget' => '</div>',
    'before_title' => '<h2>',
    'after_title' => '</h2>',
));
}

The complex option gives you the ability to name your sidebar. Here you have “left-sidebar” as your name (just in case you ever want to add a second sidebar). You have also coded what you want to appear before the widget, after the widget, before the widget’s title and after the widget’s title. So now all your widgets will appear on the front end with HTML like this:

<div class="side-widget">
<h2>Widget Name</h2>
<em>Widget stuff</em>
</div>

You can now style your Widgets to appear how ever you want by creating a style for .side-widget in your style.css stylesheet. You should also create a style for the unordered lists that will appear, since most widgets display text under a list. Add .side-widget ul and .side-widget ul li to you style.css file as well.

Next you need to add some code to your sidebar.php files to make it all functions properly.

<?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar() ) : ?>
<!-- Add your stuff here -->
<?php endif; ?>

or

<?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar(1) ) : ?>
<!-- Add your stuff here -->
<?php endif; ?>

or

<?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('left-sidebar') ) : ?>
<!-- Add your stuff here -->
<?php endif; ?>

The above will all do the same thing (but the second and third option are necessary if you have multiple sidebars).

Now all you have left to do is go into your WordPress admin section for your widgets and add some to your sidebar.

About the author:

A freelance web developer living in Montreal who spends most of his time writing for this site and building Premium themes for WordPress. You can find him on Twitter @bavotasan.

Site5 Affiliate Link
If you liked this, please share it.

Tags: , , , , , , , , , , , , , , , , , , , ,

Short URL: http://bit.ly/9pXhks

Discussion 25 Comments

  1. Leonardo B. Lopes on March 31, 2009 at 8:56 pm

    Hey, How do I add that named sidebar to my template?
    I followed these instructions and then tryed:

    Didnt work :(

    • c.bavota on March 31, 2009 at 11:45 pm

      It cut off your code. Can you please try putting it inside a <code></code> tag so I can take a look.

  2. Darko on April 10, 2009 at 2:48 pm

    I’ve followed your tutorial and registered the 3rd sidebar on my site, the left and right are working correctly, but the one in the middle displays question marks before and inside the widgets.
    Do you know what went wrong?

    • c.bavota on April 16, 2009 at 12:32 am

      Hmm. I would have to see it to try to figure out the issue.

  3. DWCouch on November 18, 2009 at 6:07 pm

    How would I specify different $args array for 2 different sidebars?

    • c.bavota on November 20, 2009 at 12:51 pm

      Two sidebars should look like this:

      if (function_exists("register_sidebar")) {
      register_sidebar(array(
      'name' => __('First Sidebar'),
      'before_widget' => '<div class="side-widget">',
      'after_widget' => '</div>',
      'before_title' => '<h2>',
      'after_title' => '</h2>',
      ));
       
      register_sidebar(array(
      'name' => __('Second Sidebar'),
      'before_widget' => '<div class="side-widget">',
      'after_widget' => '</div>',
      'before_title' => '<h2>',
      'after_title' => '</h2>',
      ));
       
      }
  4. Emily B on January 30, 2010 at 12:08 pm

    I am trying to follow your widgetize instructions down to the T, and it is riddled with tiny errors that would frustrate anyone who is not comfortable to php coding. Finally got it working now.

    • c.bavota on January 30, 2010 at 4:04 pm

      What errors did you encounter?

    • Emily B on February 1, 2010 at 1:57 am

      I would suggest including the opening and closing PHP tags of the first two coding options for the functions.php file to avoid any confusion. The 2nd error I encountered was the PHP if statements didn’t end properly.

      It took me a few minutes to figure out. I did manage to get my widgets activated thanks to your article.

    • dagpenge on January 31, 2011 at 8:32 pm

      I recently came across your blog and have been reading along. I thought I would leave my first comment. I don’t know what to say except that I have enjoyed reading. Nice blog, I will keep visiting this blog very often.

  5. malborku on August 24, 2010 at 4:41 am

    I dont have
    register_sidebar(array(
    ‘name’ => __(‘Second Sidebar’),
    ‘before_widget’ => ”,
    ‘after_widget’ => ”,
    ‘before_title’ => ”,
    ‘after_title’ => ”,
    ));

    AND is ok

  6. interim on January 4, 2011 at 7:07 am

    It is a very informative and useful post… Thank you it is good material to read this post

  7. Sander on January 10, 2011 at 2:50 pm

    Hi,

    Can you tell me where in the header i have to put this code:

    Widget Name
    Widget stuff

    And is the code OK? Or do i have to open end close it with a php or html tag?

    • Sander on January 10, 2011 at 2:52 pm

      i my message above i mean the code you have to put in the header, the 3rd code of the tutorial

    • c.bavota on January 11, 2011 at 12:35 pm

      None of that code needs to go in the header.php file. The only code that could go in the header.php file would be the call to the sidebar, ie. the get_sidebar() function.

  8. mobilt bredbånd on January 22, 2011 at 1:06 am

    Thanks for nice info. It’s useful for me. Can you give me some more information with details? I will wait for your next post.

  9. Vienna Souvenir on January 23, 2011 at 4:41 pm

    A perfect info source for souvenirs from Vienna I came to your article from another article and am really interested in this learning about this. , I feel strongly about information and love learning more on this. If possible, as you gain expertise, It is extremely helpful for me.

  10. inkasso on January 30, 2011 at 11:38 am

    Thanks for the tips, will give it a go

  11. kasumi on January 30, 2011 at 1:38 pm

    It is a very informative and useful post… Thank you it is good material to read this post increases my knowledge

     Cathrine
  12. Michael on February 8, 2011 at 4:35 am

    Hi and thank you for this nice tutorial but you forgot to give your 3 last codes an end tag when I realised why it wasn’t working.

    • c.bavota on February 8, 2011 at 2:14 pm

      You are correct, sir. Thanks for pointing that out. I just fixed it.

  13. cecilia vega on March 2, 2011 at 7:06 am

    Here you can see a lot of BDSM videos with the famous model: Cecilia Vega. The video of the most beautiful and depraved Cecilia only on our blog. Only the best clips and videos with this model. Many people say that Vega is one of the most interesting actresses of the BDSM category. Our video to prove it.

  14. long island roofing on March 21, 2011 at 5:49 am

    Hello,

    I just need some quick help with my wordpress blog. My sidebar is showing up at the bottom of the page after my posts. I don’t know how to get it back to it’s normal place.please help me.

    Regards,
    John

    • c.bavota on March 22, 2011 at 11:22 am

      You might have a conflicting plugin or maybe you added something to your post content that is causing the issue. Look at your post content in the HTML editor to make sure you don’t have any rogue tags.

  15. Fly til London on March 30, 2011 at 8:46 am

    What an awesome job you’ve done here. I simply love WordPress and all the customization possibilities it brings.

    Thanks,

    Thomas from Denmark