Over the past few months, I’ve seen the code for hundreds of WordPress themes. I’ve seen some beautiful code and some downright nasty code. One thing that I’ve seen more often than not is the same few lines for handling sidebars. This is code from 2007 and most likely copy-pasted from older WordPress themes.
I just wanted to clue an extremely large portion of the theme development community in on a little secret: sidebars have been a part of WordPress core and have seen some updates over the last three years.
With that in mind, I’m going to walk you through the steps of creating and using sidebars for WordPress themes. Maybe you’ll even pick up on some things you didn’t know about. The goal is to teach theme developers how to properly register sidebars and end this cycle of using outdated code.
What are sidebars?
The term “sidebar” actually refers to two different things in WordPress:
- Dynamic sidebar: A container for a set of widgets, which the user can set from the Widgets screen in the admin.
- Sidebar template: A theme template that displays content.
In most situations, a theme would register a dynamic sidebar and load its widgets within a sidebar template. This isn’t always the case, but it’s generally what you’ll see. It’s important to understand that there’s a difference though.
Generally, the term “sidebar” refers to a dynamic sidebar, which is the focus of this article. However, I will touch on sidebar templates as well.
One thing I’ve been disappointed with when looking at themes is that many theme developers aren’t fully taking advantage of one of the most powerful features of WordPress. Most themes will only have a single sidebar, maybe two at most. But, these themes will create large theme options pages for things that could be easily handled with widgets or put content directly in templates. I’d love to start seeing themes with more sidebars.
Registering a dynamic sidebar

Themes usually fail my quality guidelines the most in this area, so if you’re a theme developer, let’s make sure you get this right. Properly registering a sidebar is the most important part of the process because what you set here will trickle down to other sidebar functions you’ll use later.
When registering a sidebar or multiple sidebars, you would do so from your theme’s functions.php file.
The code shown below is an example of how to properly register a sidebar in WordPress using the register_sidebar() function. In this particular example, you’ll register a sidebar called primary, which will be the example sidebar used throughout the remainder of this tutorial.
<?php
add_action( 'widgets_init', 'my_register_sidebars' );
function my_register_sidebars() {
/* Register the 'primary' sidebar. */
register_sidebar(
array(
'id' => 'primary',
'name' => __( 'Primary' ),
'description' => __( 'A short description of the sidebar.' ),
'before_widget' => '<div id="%1$s" class="widget %2$s">',
'after_widget' => '</div>',
'before_title' => '<h3 class="widget-title">',
'after_title' => '</h3>'
)
);
/* Repeat register_sidebar() code for additional sidebars. */
}
?>
It’s fairly simple stuff for most theme developers, yet so many themes have a mess when you look at their sidebar registration code.
Arguments for dynamic_sidebar()
The register_sidebar() function accepts a single parameter named $args, which is an array of arguments that define how the sidebar and its widgets should be handled. In the example above, each of these arguments were manually set.
id
The id argument is perhaps the most important argument you can set (see the “Bad sidebar code” section below on why you definitely need to set this). WordPress will use the ID to assign widgets to a specific sidebar, and you’ll use the ID to later load the sidebar.
Each ID should be unique to the sidebar. By default, WordPress sets this to sidebar-$i (where $i is a count of the registered sidebars).
'id' => 'primary',
name
The name argument is the human-readable label for your sidebar used in the WordPress admin. You can set this to anything you think best represents the name of your sidebar. Generally, sidebars are given a name that lets the user know where it’ll appear in the theme.
This argument can also be internationalized. So, make sure you set a proper textdomain when preparing your theme for translation. The default for this argument is Sidebar $i (where $i is a count of the registered sidebars).
'name' => __( 'Primary' ),
description
The description argument was introduced in WordPress 2.9. It allows you to set a specific description for how the sidebar is used within your theme. This argument defaults to an empty string. It can also be internationalized.
'description' => __( 'A short description of the sidebar.' ),
before_widget
The before_widget argument is a wrapper element for widgets assigned to the sidebar. It should also be a block-level HTML element. This argument has a couple of things that you should always set with specific code so that plugins can properly use them, which is the id (%1$s) and class (%2$s) attributes.
By default, WordPress sets this as a list item: <li id="%1$s" class="widget %2$s">. I’m not a big fan of making sidebar widgets list items. I typically always go with a <div>.
'before_widget' => '<div id="%1$s" class="widget %2$s">',
after_widget
The after_widget argument is pretty simple. It’s used as the closing wrapper for widgets assigned to the sidebar. You just need to close off the element you set for the before_widget argument. By default, WordPress sets this to </li>.
'after_widget' => '</div>',
before_title
Most widgets display a title if the user sets one. The before_title argument is the opening wrapper element for the sidebar’s widget titles.
By default, WordPress sets this to <h2 class="widgettitle">. I don’t like using an <h2> in this scenario. An <h3> or <h4> seems more semantic. I’m also not sure about the non-hyphenated class name, which makes it nearly unreadable.
'before_title' => '<h3 class="widget-title">',
after_title
The after_title argument is to close the wrapper element set in the before_title argument. By default, WordPress will set this to </h2>. You just need to make sure you set it to properly match the value you gave to the before_title argument.
'after_title' => '</h3>'
Displaying a dynamic sidebar
Once you’ve registered a dynamic sidebar, you’ll want to display it within your theme. WordPress has a function for this called dynamic_sidebar().
The dynamic_sidebar() function takes a single parameter of $index, which can either be the sidebar’s id or name argument (set when you registered the sidebar). While you can technically use either, it’s almost always safer to use the id you set.
Using the below code in one of your theme templates, you can display the primary sidebar, which we registered in the previous section. Note that I also used a wrapper element so the sidebar can be easily styled.
<div id="sidebar-primary" class="sidebar">
<?php dynamic_sidebar( 'primary' ); ?>
</div>
Generally, this code would go within a file called sidebar-primary.php, which you’ll learn about in the “Sidebar templates” section later. However, dynamic_sidebar() can technically be called anywhere within your theme.
Displaying default sidebar content
Some themes developers may opt to display default content when a user hasn’t assigned any widgets to a specific sidebar. To check if a dynamic sidebar has any widgets, you would use the is_active_sidebar() conditional tag.
Like the dynamic_sidebar() function used to load the sidebar, is_active_sidebar() accepts a single parameter of $index, which should be the ID of the sidebar you want to check for active widgets.
With the below code, you can check if the primary sidebar has widgets. If so, display the widgets. Else, display some custom content.
<div id="sidebar-primary" class="sidebar">
<?php if ( is_active_sidebar( 'primary' ) ) : ?>
<?php dynamic_sidebar( 'primary' ); ?>
<?php else : ?>
<!-- Create some custom HTML or call the_widget(). It's up to you. -->
<?php endif; ?>
</div>
Collapse sidebars without widgets
The previous example showed you how to display default content when a specific sidebar is inactive. But, you also have the option of completely collapsing (not showing any content) if the sidebar is inactive.
Again, you’ll use the is_active_sidebar() function to check if the primary sidebar has any widgets assigned to it.
<?php if ( is_active_sidebar( 'primary' ) ) : ?>
<div id="sidebar-primary" class="sidebar">
<?php dynamic_sidebar( 'primary' ); ?>
</div>
<?php endif; ?>
You can actually do some pretty interesting things with this. For example, you can create dynamic widths for your content depending on which sidebars are active/inactive. That’s a tutorial for another day though.
Sidebar templates
We’ve covered everything you need to know about using dynamic sidebars. Actually, there’s some other interesting functions if you feel like digging around the core WordPress code. For now, let’s take a look at sidebar templates.
Sidebar templates are typically used to house the code for a dynamic sidebar (see “Displaying a dynamic sidebar” above). The average WordPress theme has a single sidebar template called sidebar.php. If your theme has a single sidebar, this is all you’ll ever need.
Sidebar templates are loaded within a theme using the get_sidebar() function. The code below is what you would use to load a sidebar.php template:
<?php get_sidebar(); ?>
The get_sidebar() also accepts a single parameter of $name, which allows you to load a more-specific sidebar template. For example, the code below would call the sidebar-primary.php template.
<?php get_sidebar( 'primary' ); ?>
For the best organization of your theme and separation of code, you would create a specific sidebar template for each of your dynamic sidebars. Suppose you created two dynamic sidebars with the IDs of primary and secondary. To best organize these, create both a sidebar-primary.php and sidebar-secondary.php template for handling those sidebars.
You would use the code below to load both of these sidebar templates:
<?php get_sidebar( 'primary' ); ?>
<?php get_sidebar( 'secondary' ); ?>
The above is just the convention I use for sidebar templates. You can mix this up a bit to do things in a way that best suits you. The most important thing to do is make sure you use the get_sidebar() function when loading a sidebar template.
Note that sidebar templates don’t actually have to display dynamic sidebars. They can technically contain custom-coded content that displays anything. Remember, you can display a dynamic sidebar in any template as well.
Bad sidebar code
There are some common things I would like to see changed within themes. Not all of these things are technically incorrect, but they can present some unintended consequences or are just needless bits of code.
Problem #1: Randomly dropping code into functions.php
If you’re a theme developer, you should be familiar with WordPress’ built-in hooks. Not only should you be familiar with them, you should actually be using them.
The biggest issue I see is sidebar code just being dropped into functions.php. You should create a sidebar registration function and hook it to widgets_init. You can see an example of this in the “Registering a dynamic sidebar” section above.
The reason this is important is so that child themes (and even plugins) can know exactly when a sidebar was registered. This gives child themes the opportunity unregister a sidebar if needed. Plus, not doing it this way is just plain sloppy.
As a sidenote to this: You should never just drop code into functions.php. Always use the hooks WordPress provides to execute your functions when they should be executed in the WordPress flow.
Problem #2: Not setting sidebar IDs
Many people don’t realize this but when you don’t explicitly set a sidebar ID, you’re asking for trouble. When you use register_sidebar() or register_sidebars() without setting individual sidebar IDs, WordPress auto-creates these IDs by counting the number of registered sidebars and assigning a number as the ID.
This sounds great in theory. However, there’s a huge problem. When a plugin or child theme comes along and registers a new sidebar, this new sidebar gets the ID of 1 (if registered earlier in the flow), which alters the IDs of all the other sidebars. From an end user’s point of view, all of their widgets get assigned to a different sidebar.
Utter chaos.
Widgets are assigned to dynamic sidebars according to the sidebar ID. If that ID changes, the widgets appear to shift to a different sidebar. That’s why it’s important to manually set the sidebar IDs when registering a sidebar. Properly setting the ID is shown in the “Registering a dynamic sidebar” section above.
Another benefit of manually setting the ID is that you know exactly what the ID is for use in other functions such as dynamic_sidebar() and is_active_sidebar().
Problem #3: Backwards-compatiblity checks
The PHP function_exists() check is not needed. I’ve seen this in at least 80% of themes that I’ve looked at. As mentioned earlier in this post, dynamic sidebars have been around since 2007. The only reason to use this to check for sidebar functions is for backwards compability. However, most themes aren’t actually backwards compatible. And, backwards compatibility isn’t something I’d recommend beyond one version back.
One common check is to see if the register_sidebar() function is present as shown below. Forget about this check and simply register the sidebar.
if ( function_exists( 'register_sidebar' ) )
The same goes for a check of dynamic_sidebar(). Rather than checking if this function exists, simply call the sidebar.
if ( function_exists( 'dynamic_sidebar' ) )
Some people have a different opinion on backwards compatiblity. That’s fine. But, if you’re going to code one thing to be backwards compatible, then take it the distance by making the entire theme backwards compatible.
Problem #4: Not using get_sidebar()
When loading a sidebar template, I often see this code (or something similar):
include( TEMPLATEPATH . '/sidebar.php' );
This is not the proper way to load a sidebar template. WordPress has a function called get_sidebar() for handling this. Always use it as shown in the “Sidebar templates” section above. The reason you should use this function is because a specific hook (get_sidebar) is executed when this template is loaded, which plugins might use to handle specific features.
There are cases where get_sidebar() might not be appropriate for a specialized theme, but it’s something I’ve rarely seen.
Let’s update those sidebars
I’ve been reviewing some of the themes submitted to the WordPress themes repository over the last few months and figuring out how the theme review process works. I’ve literally looked at the code of 100s of themes in this time. Nearly every theme I’ve seen has at least one of the issues I described in the “Bad sidebar code” section of this article.
The goal is to help new and veteran theme developers create better-coded themes. This is only a single issue of many, many common issues I’m seeing in themes. I’ll probably write more on other issues in the future, but for the moment, I hope this article will help theme developers clean up their sidebar code.
Luckily all our themes we’re building are build off some awesome framework called Hybrid Core.
Of course, that’s the easiest way to do it.
Oh yes, theme developers, please, read this post!! I completely agree with it, some themes lacks on define the sidebars properly and it is no so hard to do things in the correct way. This is a great tutorial, and a must read for a theme developer.
I made a plugin (custom sidebars) for creating all the sidebars you need easily from the admin area, a functionality that i think WP should have out the box, and its main issues are related with themes that doesn’t use correctly the dinamic sidebars.
Wordpress has a great potential with its widgets and sidebars, we can push it much further.
That’s been my biggest issue as well. Sometimes I create plugins for clients that work with the sidebar system and end up having to fix their theme too.
excellent tut, as usual. but shouldn’t ‘after_widget’ close the tag, like this:
'after_widget' => ''make that:
'after_widget' => ''</div>"(you have a tag opener:
<div>)Yep, good catch! I updated the post to correct this.
Justin,
Thanks for the information. Its always interesting to get the opinions from some of the main developers as to what could be done better.
I consider myself more of an intermediary developer. I have a pretty good handle on how WordPress works, a fairly good understanding of PHP as a language and the ability to research things I am unsure of. Recently I began working on my own framework for the many projects I develop, and in doing so decided I wanted a framework I could be proud to share with others. I wanted to make sure the coding was clean and used the most up to date techniques and functions WordPress offered. A lot of what I have used in the framework I found in the TwentyTen theme.
I personally would LOVE to hear more about the issues you are seeing in WordPress themes. While I do not feel my framework is quite ready for release to the public yet, I am striving to make sure it is of top quality and this type of feedback is great!
Thanks again.
Ed
I’m putting together a list of some common mistakes I see as I go along. I just have to find some time to write about them.
Justin,
I was just doing some research on something I want to do with widgets, and look what I ran across:
http://codex.wordpress.org/Widgets_API#Display_Sidebar_on_Theme
Here, the WordPress Codex gives the exact “if(function_exists)” setup that you mention as outdated. This may be a big reason you are seeing this so much still… the basic WP documentation still references it.
Maybe this should be addressed?
Ed
Thanks for pointing it out. I just updated that page. It’s a wiki, so if you have an account at WordPress.org, you can also update things like this.
On that note, what I am looking to do with widgets is something similar to what you were trying to do a year or two ago (damn, you are so far ahead of me!)
For styling purposes, I want to allow each widget to have its own unique class when the user adds the widget. Since I won’t know what the widgets will be, I can’t call the specific widgets in the css. It seems you were looking for a way to add even and odd classes to the widgets. Did you get anywhere on that? Or, even better, do you know of anything (code or plugin) that will allow a user to add a specific class (possibly by dropdown, field, etc) to each widget as they are adding the widget?
I use the Widget Logic plugin quite a bit, so ideally something similar that would allow the addition of a class (such as “red”, “dark”, “highlight” or similar terms added to the css) that would specify how that one widget should look would be perfect. Any help or push in the right direction would be helpful.
Thanks
Ed
Ed, you can style each widget differently without using a plugin, yes, as long as you correctly follow the steps detailed in this post by Justin.
Look at this code:
The above code creates a
divcontainer that wraps the content of the widget. This code also assigns a unique id to every widget, so if I add a text-widget in the primary sidebar, WordPress will assign it a unique id to the containerdivthat wraps the widget. If you view the source of the new created text-widget you’ll see an HTML code like this:Now you can open your
style.cssand style the widget with the idtext-3.@jamal: I am aware of this option, but that does not allow the user to apply a class on the fly.
Let’s say that in my CSS I have 2 classes for sidebars. One would have a dark frame, the other a light frame. If I am developing a theme that anyone could use, I won’t know going in that they will be using a specific text widget and want that widget to have a dark frame. So I would not know ahead of time which ID would be applied to which widget.
I could use multiple sidebars, and with each sidebar add a unique style. However, what I am envisioning is allowing the user to decide as they add the widget how it would be displayed.
I know that in using the Widget Logic plugin, this plugin adds a box that allows variables to only show on certain pages using functions such as is_front_page() or is_page(8). However, I would love for the user to also be able to add a specific class in the same manner.
So since I do not know what the widget ID will be ahead of time, I need something flexible that allows the assignment of a predetermined style class to be applied as the widget is added.
Anyone with this solution, I would love to see it!
Thanks
Ed
Hey Justin,
I know you are a busy guy, but I was wondering if you have any comment on adding specific classes per widget. I would love to find a way to make this happen… any thoughts?
Ed
It’s well outside the scope of this particular tutorial and the comments form doesn’t really make for a good area to describe this process. It can definitely be done though.
Take a look at the
WP_Widget_Linksclass inwp-includes/default-widgets.php. It has an example of how to add in a custom ID, which is basically the same as adding a custom class.Justin, as always you have provided clear direction.
Thank you so much for the excellent “tutorial”.
One of the shortcomings I’ve found with the args to these include functions is the lack of support for subfolders. Things get pretty messy with a big site when your forced to namespace your filenames rather than organize your includes into folders.
Any thoughts on this?
Yep, that’s pretty much one of the use cases I had in mind. For the proper function in that scenario, you should be using locate_template().
@Justin, great article as usual!
@Ed: I’ve done some research on “custom syle” for widget too… and while we can easly hook in the widget admin screen to ad checkbox or dropdown for adding options, it looks like the “show widget” logic doesn’t contains any hooking capability to add custom tag or anything else.
The logic that display the vidget simply outputs the “before & after widget”, “before & after title” with an echo statement, so you can’t hook neither there.
Maybe I’m missing something… Justin?
Stefano
This is a great article. I really like when people mention ways to customize and work with the “native” features of Wordpress. Many developers seem to think they need a 3rd party plugin for unique content areas which could easily have used a custom sidebar. I see that even in expensive themes.
One last note on why to use “get_sidebar” instead of include is because WP will gracefully default if that sidebar template is missing for some reason which is better than seeing php errors on the page.
It is funny how slightly misleading some of the WP naming conventions can be. I wonder if it were called “dynamic_module” it would be more realized these can be anywhere and even in multiple locations. Same goes for Custom “Post” Types. It really isn’t a “Post”.
Yes, that’s definitely another reason to use
get_sidebar().My main gripe isn’t with third-party plugin things. What I don’t like from themes is seeing customizable areas for the theme that would be much better handled with widgets. I see themes with advertisement management areas that would be better as a sidebar with a custom ad widget.
As for the term “sidebar,” I’m not a fan of it. I’ve made my arguments and moved on.
Thanks for another great eye-opener Justin.
And, as previously mentioned by Barrett, would not the div-tag be “closed”? Or have i overlooked something?
[code] 'after_widget' => ''"[/code]
Yes, it should be closed. I updated the post.
Hey Justin. Thanks for your post. I always find your posts to be thorough and straightforward. They have helped me to make my custom WordPress framework theme much better.
In this case you busted me on 2 of the bad examples (unnecessary backwards compatibility check and not using IDs). So it’s back to work I go. Thanks again.
I’m glad I could be of help. Setting up those IDs is huge though. It’ll save you some support questions down the road.
@Stefano if I could access the widget interface to add a drop down that could be attached to and called with each unique widget, I could use it to add a div around the widget, correct? That would give the ability to target widgets within that class, right?
I am not sure how to access the widget interface so if you can point in the right direction it would greatly appreciated!
No, this is not the Yet-Another-Blog-Post-About-WP-Sidebar
This is the best one !
Thank you very much for all the comprehensive WP articles here.
I was just making my sidebars for the first time, so this was right on spot. Thank you!
I especially found the “Collapse sidebars without widgets” part helpful for allowing empty sidebars to validate when using unordered lists as the widget containers.
I hadn’t even thought about invalid lists since I always use a
<div>. That’s definitely another benefit.I was recently thinking why people check if the function
register_sidebar()exists, which was present in the WP core since 2.3 as I guess. This is something I guessed you’ll talk about when you mentioned “bad sidebar code” earlier in this post.Also I’m against this aimless backward compatibility check which really harms the way we develop themes and plugins. In this case, we’re only helping hackers by making end-users stick with their old WP installations.
As for using
<li></li>tags inbefore_widgetandafter_widget, I’d choose the<li></li>over the<div></div>, as the former one seems more semantic and can support Microformats easily.There’s definitely some different opinions over whether to use
<li>or<div>. I’ve always just been a fan of the latter.Another great post. Thank you Justin.
I’ll be very interested to read your other future related posts like how to “create dynamic widths for your content depending on which sidebars are active/inactive” and what other common mistakes you’ve found with themes.
I’m working on a new article now that covers something even worse: theme settings.
Excelentes dicas nesse blog adorei….
I’m not a designer of theme but I’m interested in what you said to be: downright nasty code. What does that mean and how do I know.
Thank Justin
I’m writing tutorials on fixing this nasty code in themes. This is just the first of many. Just stay tuned, and I’ll keep the articles coming.
great post Justin, informative and straight to the point.
Useful post about sidebars, thanks for sharing!
Is there a way to have sidebars per posts? A major dealbreaker for me, is the lack of adding widgets on a per post basis (for example info boxes, relevant entries etc). WordPress is a very fluid and dynamic publishing tool, but lacks basic features like this.
How would you solve this?
Yes, there are several methods and plugins available for handling contextual sidebars and widgets. A quick search on your favorite search engine will turn up plenty of these things. Context-based sidebars are a bit outside the scope of this tutorial.
As for this being a “basic feature,” it’s most definitely not one. The idea is basic, but the code and user experience issues are extremely complex.
Great article – thanks, Justin. @Ronny, I just published this super simple graceful sidebar plugin which enables you to create custom sidebars for your posts or pages. Simply install the plugin, create two custom fields in your post called “graceful_title” and graceful_content” with the title and content you wish to have displayed in the sidebar. Next, drag the Graceful Sidebar Widget to your theme’s sidebar and you’re good to go.
Enjoy!
I have created some widgets.By using the default theme of wordpress, I am able to show those widgets either in sidebar or footer. What I need is to show them in the centre..that means where we normally place the contents, but I don’t know how to do…I think I should customize this template.I don’t want to use some other themes, rather I would like to use the default twenty ten theme. Can anyone help????
Great article thank you! Will all the tools available to make themes a lot of creators are falling short when it comes to sidebar content and management.
What if a widget has no title and I want to skip the ‘before_title’ and ‘after_title’?
@SlowX The way that widgets are displayed depends on each widget particulary, so if they have no title some of them will display the before/after title code, but others don’t.
Notice that before/after title are properties that belong to a sidebar, not a widget, you will have to define for the sidebar, so you don’t know when its widgets will have title or not.
Having a look at the code i realize that there is an action hook which is called before the widget is printed
maybe using it you will be able to disable all the befor/after title stuff.
@Justin, I have linked this tutorial from inside my sidebars plugin, because it rocks!!
http://wordpress.org/extend/plugins/custom-sidebars/
Cheers!
Here is an excellent page for the sidebar. Have found exactly what I search for correctly Widgeting a template
Thank you for this excellent work.
Amazing article. Thanks so much for taking the time to write this!
hi,
I am using a theme from WPSHOWER and I just noticed that the sidebars do not have an ID.
here is the code:
Can I add an ID manually now that the site is already running with a lot of pages and posts or will it just ruin everything?
And thanks for the tip.
Maga
You can definitely add in sidebar IDs yourself. The only thing it’ll “ruin” is your widgets for those sidebars. You’ll have to add those again because they’ll disappear.
Also, please feel free point your theme developer to this tutorial.
So, now that the function ‘unregister_sidebar’ is deprecated, how do I unregister it in a child theme short of deleting the register code in the parent theme?
The
unregister_sidebarfunction is not deprecated.Hi Justin,
I have a theme that I inherited, so I don’t fully understand everything in it.
In functions.php, there is a call to register_sidebars with a single sidebar definition, and this has the correct format, so looks fine.
There are two sidebar template files called sidebar.php and leftsidebar.php. These are added to the theme in index.php using 2 calls to locate_template, first with leftsidebar, then with sidebar.
Both of these files have a call to dynamic_sidebar (as well as a bunch of unrelated stuff). The one in leftsidebar is the one that is defined in register_sidebars in functions.php.
The strange thing is that both sidebars show up in the admin, and I can add widgets to them both. So the question is, why does the second dynamic_sidebar work, even though there is only one sidebar defined in register_sidebars in functions.php ?
I am incredibly grateful to you for the clarity of this post. As soon as I clear my plate, I am going to read through all of your posts! PLEASE keep writing!
Umm, is there any plugin to install and do all that to me?! I mean I’m not into
so it won't be so easy for me...Note: The section with the heading “Arguments for dynamic_sidebar()” actually describes arguments for register_sidebar().
Justin – I’m not a developer so I’m encountering a problem I’m having trouble solving.
I have a dynamic sidebar and I want to style the “Archives”. I don’t know where to look for the actual code for the “Archives” as it displays from the widget to my sidebar.
Make sense? Any help would be greatly appreciated!
Great article btw – thanks!
This is fantastic! But when I register the new widgets, in the back-end, the widget handlers are automatically added to the top. In other words, when I go into appearance –>widgets, those show up but at the very top. Is there any way to control what order they do in?
I appreciate your help.
Hi Justin,
Great tutorial – Thank you so much! I am new to Theme Development and I was overwhelmed with the number of different ways to create a diff sidebar for diff pages. Thanks for taking the time to clarify the good/bad code!
I followed the instructions and my new sidebar appeared on the page, woohoo! I am experiencing a problem though
Now, if I edit a page and hit the ‘update’ button, the screen goes white, while the URL redirects to /wp-admin/post.php. The screen stays white, until I hit the back button & changes are not saved. I log out & when I try to login the screen is white.
I am developing a child theme for WP Twenty Eleven. So I:
1. Registered the new custom sidebar in a new functions.php in my child theme folder
2. Added dynamic_sidebar( ‘sidebar-custom’ ) in new file sidebar-custom.php
3. get_sidebar( ‘custom’ ) in pages.php
It seems to me that the functions.php file conflicts with the Parent theme’s function.php file – because when I remove it the site works fine again…but of course no dynamic sidebar…
I would much appreciate if anyone has any suggestions for me. Thanks heaps in advance.
Is there a simple way to place our newly created sidebar using a hook? Rather than editing the template file, can we register the sidebar and attach it to a specific place in your theme? All in a functionality plugin.
Peter
Hi Justin,
I just a new guy in the Wordpress world.
I try to fix the sidebars with the : { position: fixed; } code line.
It was good only for one sidebar, but I want to fix also the second one, because I use right and left sidebars. So when I use the code for the second sidebar, the template changes to only one sidebar. I work with the Weaver II Pro.
Maybe you have some idea about it ?
Thanks a lot,
Jacob
Thanks Justin!
The codex can sometimes be as clear as mud.
Without bloggers like you, I would be completely clueless instead of somewhat clueless… ^_^
many thanks to you.
that was very useful.
Great post. Thank you so much. Among all the posts I’ve googled for creating sidebars, this one is probably the best explained and covers pretty much everything.
Good post.
I was looking at this as I built a child theme on twentyten. I notice in that theme, they call two sidebars in the same sidebar.php file. Of course, they don’t really function as two sidebars from the perspective of a viewer or even a user of the theme — since they are more or less just stacked on top of each other.
Any idea why it’s set up that way?
What template a dynamic sidebar (i.e., a widget area) is called from is really irrelevant in terms of properly setting them up. The theme could have just as easily put the dynamic sidebars wherever it calls
get_sidebar().Just keep in mind that dynamic sidebars and sidebar templates are separate entities. It makes sense for me (probably for you too) to have sidebar templates match dynamic sidebars. I’m personally not a fan of how it’s done in TwentyTen, but it’s definitely a valid choice.
So if you weren’t going to use widgets in a sidebar (in a custom dedicated theme for something) you wouldn’t have to register it as a sidebar at all, right?
You could then just use get_template_part to call it onto a page? Or would that be considered not good practice?
Thanks again for a great blog. Glad I found this.
Correct. You only register dynamic sidebars, which are the things that hold widgets. Sidebar templates are just PHP files.
As far as using
get_template_part()is concerned, only use it for template parts that don’t have existing functions. So, if you consider your template a “sidebar,” the appropriate function to call it with would beget_sidebar(). Both would work, but there are specific hooks that some plugins may use. Therefore, you always want to use the proper function.Why are these called sidebars? They should be called widget areas.
I have placed these in the header and footer not just on the sides of the page.
Justin, maybe this is the wrong place to ask it, but you mention just dropping functions in the functions.php without adding actions. But in child themes, adding actions results in errors. So is just dropping the functions in okay the? or should we use filters or alternative action commads there?
All of the above applies to any type of theme, even child themes. Adding actions in child themes does not result in errors. Incorrect code results in errors.
thank you for this tutorial! I’m a theme developer and a WordPress newbie and this tutorial is a real eye-opener as another commenter said before, thanks again, it helped clarify some of the issues I didn’t understand before, this page has been bookmarked!
Thanks again
You win! Great article, I fixed my problem. Following tutorials that never get updated are there full of errors had me searching everywhere for a sidebar tutorial. Thanks!
Great tutorial – and I’m reading it nearly two years after it was written.
Thanks!
Great article – good reading!
Just a little confused though. You mentioned that for dynamic menus you need to call them via the dynamic_sidebar() function; however then further down you mentioned that..
“For the best organization of your theme and separation of code, you would create a specific sidebar template for each of your dynamic sidebars. Suppose you created two dynamic sidebars with the IDs of primary and secondary. To best organize these, create both a sidebar-primary.php and sidebar-secondary.php template for handling those sidebars.
You would use the code below to load both of these sidebar templates:
”
So I am confused….. which do I use to display a dynamic sidebar?
Thanks!
You would use
dynamic_sidebar().Well, I came here to see if I could find any information on why your My Life template won’t show the secondary sidebar, no matter what I’ve tried. Unfortunately, I don’t see anything that helps. Rather than show the sidebars next to each other, they’re showing the main sidebar at the top of the column and the secondary sidebar below that.
Really lovely template, but if I can’t get a three column layout, I can’t use it.
The My Life theme is supported at Theme Hybrid. You can get whatever assistance you need in using the theme there.
That’s okay. I thought your post above might give me some insight, but everything checks in the code. I didn’t do the Theme Hybrid site because it’s $29 a year and I just wondered about this one thing.
It really is a very nice template, like I said, lovely design. I just can’t use it then. But thanks!
Great tutorial, however I’m having a problem with WP ver. 3.4.2….
Even though I am certain that my code to register the sidebars is correct, and the sidebars are getting registered fine (and allow me to put widgets where I want them), they are not rendering correctly.
I’m using DIVs, not LIs, as you show above, but the WP code is still rendering my widgets wrapped in the li tag, not the div as I’ve specified…..
This is very frustrating…..any suggestions?
Sorry, I just figured out that I used a hyphen in place of an underscore, making my own problem…..that’s what I get for typing instead of copying & pasting…sigh….
Thanks again for posting this tutorial.
Awesome article as usual. I really enjoy reading your stuff. It is incredibly well laid out and never wastes words. THANKS!
This is a great resource if you use Wordpress as a CMS. The steps were detailed and worked flawlessly in my implementation. Thank you for your hard work!
Excellent Tutorial. I’ve been trying to find one like this that takes me through step-by-step for some time now and this was it!
On a side not, I’m not sure if you have already created a tutorial on this but I would like to know how to enable the ability to choose which sidebar you want to display on individual posts. If you have one that you could send me a link to or are willing to write one, that would be awesome! Thanks!
How to develop an unlimited sidebar function for wordpress theme?
this is awsome thank you, followed lots of vidio tutorial and were not helping. when I found this tutorial trust me I am the worlds hapiest man. thank you
This is very informative. Everything I need to know about sidebars I find here. Thank You!
I am FAR from a whiz at WP so here goes and pls pardon my ignorance. I have repeatedly read that the only way to add a second sidebar is to initially edit the functions.php – doing that totally froze my site. I had to go back and edit. So – if I want to add a 2nd sidebar directly under the current (I’m using Soho Serenity theme) can I do that without creating and uploading sidebar2.php?
How do I tell the stylesheet what to do with the new widgets? They are all floating off center.
I would like to style them now
Awesome tutorial! This tutorial was very easy to follow and saved me from hours of stress.
Good post!
Excellent article. Good to know I’m doing thinks the way it should be done.
I’ve got one question, I’ve registered a new sidebar and created a file called sidebar-main.php. I’m currently working on a childtheme of twenty twelve. Is there a way I can change get_sidebar() to get_sidebar( ‘main’ ) without copying all the relevent files to my child theme and editing thing, something I can use in my functions.php file
Many thanks