888888.888888.88""Yb..dP"Yb..8888b..Yb..dP.88b.88....db....8b....d8.88..dP""b8..dP"Y8
88__...88__...88__dP.dP...Yb..8I..Yb.YbdP..88Yb88...dPYb...88b..d88.88.dP...`".`Ybo."
88""...88""...88"Yb..Yb...dP..8I..dY..8P...88.Y88..dP__Yb..88YbdP88.88.Yb......o.`Y8b
88.....888888.88..Yb..YbodP..8888Y"..dP....88..Y8.dP""""Yb.88.YY.88.88..YboodP.8bodP'


88b.88.888888.888888.Yb........dP.dP"Yb..88""Yb.88..dP
88Yb88.88__.....88....Yb..db..dP.dP...Yb.88__dP.88odP.
88.Y88.88"".....88.....YbdPYbdP..Yb...dP.88"Yb..88"Yb.
88..Y8.888888...88......YP..YP....YbodP..88..Yb.88..Yb

February 27, 2010

As I might have mentioned once or twice, January is theme month at Fun with WordPress and I already have some interesting posts lined up thanks to everyone that has gotten in touch. For today’s post I have been asked to give a quick guide to adding dynamic sidebars(the widgety kind) to a theme, in particular horizontal sidebars.

There are three parts to adding extra sidebars to your theme:

  1. Register the sidebars in functions.php
  2. Call the sidebars form the PHP
  3. Add some extra styling to layout the sidebar

Register the sidebars

Registering the sidebar is about more than just telling WordPress it exists. You also need to specify the structure of the sidebars. In this case I want to use a structure something like this:

widget layout

The wrapper div will be contained within the theme itself as it will have a specific ID based on its location. The remainder is specified in the array shown below:

//create the array
$sidebar_array = array(
‘before_widget’ => ‘<div id=”%1$s” class=”widget %2$s”>’,
‘after_widget’ => ‘</div>’,
‘before_title’ => ‘<h2 class=”widgettitle”>’,
‘after_title’ => ‘</h2>’,
);

In this you can see that the HTML specified in before_widget and after_widget wrap the widget in a DIV tag. %1s and %2s will be automatically replaced with the ID and title of the widget so they can be individual styled if necessary. before_title andafter_title wrap the title in H2 tags. The content will be specified by the widget.

Except for this styling there is only one more variable we need to provide that is the name of each sidebar. As this is separate for each of the sidebars we will need to modify the array twice. Each time just before the sidebar is registered with theRegister sidebar function. For example:

//set the name
$sidebar_array['name'] = ‘Sidebar’;
register_sidebar($sidebar_array);
$sidebar_array['name'] = ‘footer’;
register_sidebar($sidebar_array);

With these two sets of code included in functions.php you should now have two named sidebars available for widgets. But don’t add them yet.

Call the sidebars

The next step is to add the function calls in the HTML files. The function call itself is very simple: dynamic_sidebar(sidebar_name);, but for most themes you will want to do a little more to cope with situations where there are no widgets added, and to highlight where the widgetised areas are.

If there are no widgets then dynamic_sidebar will return false. This means you can easily add it as the equation in an IF statement like this:

<div id=”footer”><?php if ( !dynamic_sidebar(‘footer’) ) { ?><p>Sample Content</p><?php } ?><p>&nbsp;</p></div>

In this example I have included some example content that will be shown if there are no widgets. In the default theme, and most others, this will contain a pre-widget sidebar. The default theme also checks to see whether widgets are installed at all. I am assuming they are, but check sidebar.php in the default theme to see how to check.

I have also added the wrapper DIV I mentioned above, this one for the footer, and a paragraph with a class of clear, so I can float the widgets and use this to clear them.

Styles the sidebars

wpfooter

If you go ahead and add some widgets to the footer sidebar now you will find that they all appear above one another, like a normal sidebar. This is not what we want for a footer, so a little styling is needed. But only a little.

#footer div.widget
{
float:left;
margin-right:30px;
margin-left:30px;
}

#footer p.clear
{
line-height:0.001;
font-size:0.001em;
clear:both;
}

The CSS is really simple. If you look back at the initial array and widget layout diagram you see that each widget is surrounded by a div with the class of widget. This CSS floats each widget to the left if they are within the footer wrapper, and the paragraph with a class of clear makes sure the footer grows with the height of the widgets.

The example above is the default theme. To make it work in the default theme I also needed to alter another line of CSS. Line 83:

//from
background: #eee url(‘images/kubrickfooter.jpg’) no-repeat top;
//to

background: url(‘images/kubrickfooter.jpg’) no-repeat bottom;
//create the array
$sidebar_array = array(‘before_widget’ => ‘<div id=”%1$s”>’,'after_widget’ => ‘</div>’,'before_title’ => ‘<h2>’,'after_title’ => ‘</h2>’,);

Charcoal Theme Available For Wordpress
Charcoal theme?
2 comments
page 959
Html 5 Gallery
Html5 wordpress?
6 comments
page 1305
Using Your Own Url Shortener
Htaccess short url wordpress?
4 comments
page 1190
Dont Mess With My Toot Toot
Dont mess with my toot toot dates?
15 comments
page 599
Dont Mess With My Toot Toot
Dont mess with my toot toot?
15 comments
page 599
Should Wordpress Produce A Php 5 Only Version
Compare php5 versions?
11 comments
page 35
Quick N Dirty Comment Stats
Dirty comments?
no comment
page 130
Why I Ditched Disqus
Better than disqus?
5 comments
page 1175
Html 5 Gallery
Html5 photo gallery?
6 comments
page 1305
Html 5 Gallery
Making a photo album in html 5?
6 comments
page 1305
Post Image The Easy Peasy Way
Getpost image in wordpress?
26 comments
page 1065
Ithemes Essence Review
Review of changing the essence?
4 comments
page 238
Quick N Dirty Admin Login Screen
Login screen css?
no comment
page 128
Adding Settings To Admin Pages
Writing wordpress admin pages?
3 comments
page 793
Using Your Own Url Shortener
Use your own url from link?
4 comments
page 1190
Custom Hooks For Admin Pages
Wordpress post form admin pages?
one comment
page 430
How To Add Sidebars To A Theme
Div above footer wordpress?
10 comments
page 1053
Html 5 Gallery
Html5 photo gallery?
6 comments
page 1305
Post Image The Easy Peasy Way
Image loop for wordpress?
26 comments
page 1065
Using Wordpress As A Php Framework
Wordpress framework?
2 comments
page 335
Html 5 Gallery
Html 5 photo?
6 comments
page 1305
Using Wordpress As A Php Framework
Using wordpress as a framework?
2 comments
page 335
Fun With Sidebar Tabs Styling
How to alter sidebar css?
2 comments
page 336
Should Wordpress Produce A Php 5 Only Version
Wordpress 3 php version?
11 comments
page 35
Using Your Own Url Shortener
How to make own url short site?
4 comments
page 1190
Html 5 Gallery
Html5 wordpress theme?
6 comments
page 1305
Post Image The Easy Peasy Way
Wordpress function to call images with urls?
26 comments
page 1065
Dont Mess With My Toot Toot
Wpinsertpost?
15 comments
page 599
3 Ways To Speed Up Your Blog Without A Cache Plugin
Ways to speed up wordpress blog?
one comment
page 1321
Using Your Own Url Shortener
Create htaccess for shorten url?
4 comments
page 1190
Using Your Own Url Shortener
Wordpress using bookmark for url?
4 comments
page 1190
Theming Habari Vs Wordpress
Habari admin for wordpress?
13 comments
page 440
Theming Habari Vs Wordpress
Habari post variable author name?
13 comments
page 440
  1 query every 1228 seconds, updated 1 seconds ago.
Monday, 4am
 __
(__)
   `

 sweet

how can I customize the content of the tab with this plug-in? Thank you!

Wednesday, 5pm
 __
(__)
   `

 Optimize Your Wordpress | Static-Romance.Org

[...] 50 Beautiful Free Wordpress Themes How I Learned To Build Wordpress Themes How To Add Sidebars To A Wordpress Theme My Wordpress Cheat [...]

Thursday, 1am
 __
(__)
   `

 conorp

Great post, going to try it later.

Conorp

Tuesday, 9pm
 __
(__)
   `

 WordPress Links – Week 2 2009 | WPStart.org – WordPress themes, plugins and news

[...] How to add sidebars to a theme [...]