Site icon Leonid Mamchenkov

WordPress dynamic sidebars : take it easy with widget control

One of the coolest things about WordPress is a dynamic sidebar. Dynamic sidebar is a special are defined by a WordPress theme, where widgets (blogroll links, recent comments, related posts, etc.) can go.  The beauty of it is how simple it is for the theme author to define and style such an area, and how simple it is for the theme user to configure which widgets go where.  One line of code for the theme designer, and a user-friendly administration interface with drag-and-drop support for the end user.

As simple as it is, I’ve seen it abused by theme authors one too many times.  Consider an example (borrowed from a really nice theme in the wild, which I won’t name):



What happens here is the following: in line 3 we check if this version of WordPress supports dynamic sidebars (these were added in WordPress 2.2.0, if I remember correctly). If they are supported, we insert all widgets that were dragged and dropped into ‘east_sidebar’ dynamic sidebar, using WordPress administration.

Now for the tricky bit. If the function does not exist or something went wrong while displaying the widgets, we fall back onto some defaults. In this case we show the list of categories.

What’s so tricky about it? The trick bit about it is the return value of the dynamic_sidebar() function. It returns a boolean value. True if the sidebar was found and called, and false if sidebar was not found or was not called. This “was not called” actually means “failed to find at least one proper widget”.

What happens in practice is this. You find this nice theme that you want to use for your blog. You install and preview your site. Everything looks good. The sidebar looks good too, but you don’t particularly like the selection or the order of the widgets. So you go to the administration interface and what do you see there? An empty sidebar. Instinctively, you drag-and-drop a widget into the sidebar. Refresh your site and you see something totally unexpected. The sidebar that had a bunch of widgets now only has one – the one you drag-and-dropped. Weird.

That is because when you installed the theme, it registered a new sidebar, which is by default empty. When the sidebar of your site is displayed, dynamic_sidebar() function returns false and the theme falls back onto default widgets in the theme. Once you have at least one widget dropped into sidebar, that doesn’t happen any more and your lonely widget is displayed now.

That’s not a big problem with simple themes, which have only one or two dynamic sidebars. However, more complex themes with four, five, six, and more sidebars become annoying. Because there is usually some default pre-selection of widgets for each of those sidebars, and in case you don’t like it, you’ll need to do a lot of work in that administration.

Even worse, if you want to leave some of those sidebars empty. If there are defaults set in the theme, the only way for you to silence the sidebar is edit the theme source code. In the example above that would removing everything from line 4 to line 12. If you have more than one sidebar that you want to silent, you’ll need to repeat the procedure for every one of them.

So, what’s the solution? What’s the right way? To be honest, I don’t know. From the point of view of somewhat experienced end-user, I’d prefer no default widgets in the theme source code. Let them come up empty by default – I’ll drag-and-drop whatever I want using the user-friendly administration. On the other hand, from the point of view of non-experienced end-user, empty sidebars might confuse the heck out of me. I need to know about widgets to solve the problem, and unfortunately not all WordPress users do. Also, I guess it’s a bit harder for the theme authors to promote their themes if they come up with empty sidebars once installed.

One solution that might work is a theme option. Show default widgets by default, but allow to silence them all from the theme controls. That would work as a compromise and avoid unnecessary source code editing. Also, I think it would be pretty straight forward to implement (check for the value of the theme option instead of the return value of the dynamic_sidebar()).

What do you think?

Exit mobile version