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?

Transformers: Revenge of the Fallen

I just came back from the premier of the “Transformers: Revenge of the Fallen“. After seeing the trailers for this film, my expectations were so high, that I was actually worried if the movie is going to be any close to what I wanted it to be.  I happy to report that all my expectations were bitten down and torn apart.  The real thing was so much better.

Now, I don’t want to spoil it too much for those of you who haven’t seen it, but let me give you this.  There is non-stop action from start to end.  Everything flies, swims, shoots, explodes, collides, and transforms.   There were quite a few things on the scale that I haven’t seen before. Epic!

If I had a hat, it would be off, with all respects to Michael Bay and Steven Spielberg.  And, of course, everyone else who worked on this film.  It was so much worth it!

My rating: 5 stars.  Highly recommended, must see.

P.S.: I don’t know why everyone is so turned on about Megan Fox.  She’s not bad, but I don’t think she’s worth all the hype.  Isabel Lucas (another hot girl in this film) is at least as good. Sometimes even better.

Knowing Open Source community pitfalls

I came across this excellent blog post – “Recognizing and Avoiding Common Open Source Community Pitfalls“.  It’s very related to the short speech I gave recently over at 3rd Ubuntu / Fedora release party.  My angle was on how much good participating in an Open Source project could do to a college student.  I was talking more about how much practical stuff one could learn from existing projects – their tools, approaches, etc.

The blog post talks about roughly the same, but from a different perspective.  It’s more for those who are starting a new project, rather then joining the existing one.  There are plenty of ‘myths’ that go around about how is it is to do Open Source and how everyone can do it, and that all you need to succeed is to start.  That’s not so true, of course.  There is plenty of stuff to be learned and lots of work to be done.  If you are about to start your project or started one recently or just thinking about it, read the article.  It’s has roughly a paragraph about each of the following:

  1. Built it and they will come.
  2. Your community will help you build HARD DIFFICULT FEATURE X.
  3. Directional fallacies.
  4. Contributions are free.
  5. Profit models.
  6. Folks can understand your code.
  7. Documentation updates.
  8. Everyone using App X will give back freely and without your asking.
  9. Contributors are like coworkers.
  10. Resources grow on trees.
  11. Cross distribution support is easy.
  12. Users help users and it’s ok to just be a developer.
  13. Cross-project collaboration is easy.

Game research : pottery, writing, alphabet

I have a few more words to add to the yesterday’s post “History of the world through a game“.  Something that bothered me in several games, but something that never annoyed me enough to look up or express – the connection between pottery, writing, and alphabet.

Pottery, writing, alphabet
Pottery, writing, alphabet

First of all, why is pottery a required research before writing and alphabet?  They seem to have nothing in common.  But yet in many history-related strategy games it is so.  I never thought much about it, but it just felt wrong.  Until today, when I was speaking about this with my wife and she mentioned pottery shards.  I’ve heard about them a few times, but never associated pottery with writing.  Here is a quote from the almighty Wikipedia for you:

Unglazed pottery shards were used almost as a kind of scratch paper, as ostraka, for tax receipts and, in Athens, to record the individual nominations of Greek leaders for ostracism.

This one is clear and out of the way.

The second thing that bothers me always is the order of research for writing and alphabet.  In my silly head, you need the alphabet to write.  Alphabet is what separates writing from drawing.  Here is the Wikipedia definition of “writing”:

Writing is the representation of language in a textual medium through the use of a set of signs or symbols (known as a writing system). It is distinguished from illustration, such as cave drawing and painting, and the recording of language via a non-textual medium such as magnetic tape audio.

And here is the Wikipedia definition of “alphabet”:

An alphabet is a standardized set of letters — basic written symbols — each of which roughly represents a phoneme in a spoken language, either as it exists now or as it was in the past.

Which one comes first – alphabet or writing?  I don’t know, but it seems that the alphabet is a required research for writing and not the other way around.

What do you think?

Understanding WordPress database in 10 minutes

Almost two years ago I wrote a blog post titled “A look inside the WordPress database“.  While a lot of people enjoyed it (and, apparently still do, even though it’s a bit outdated), I think it could be greatly simplified.  And it will probably take you less time to understand WordPress database now than it would take you to read through that blog post back then.

For the simplified approach to the WordPress database, you’ll need three things and three things only.

  1. Database description – WordPress Codex page.  The main thing to pay attention to on that page is the database diagram.  If you can’t grasp it all at once – DON’T PANIC – there is a description of each table further down the same page.  But trust me, you don’t need that just yet.
  2. Fresh installation of WordPress.  And by fresh I mean the one that you just did, complete with database setup and all, but which you haven’t touched yet – no options changed, no posts or pages published, no comments moderated.  Virgin WordPress.
  3. MySQL client.  And you can use whatever suits your fancy.  Command line, PHPMyAdmin, MySQL Query Browser, or anything else.  The more comfortable you are with it, the better.

Have you got everything?  OK.  Now you’ll just need to use that MySQL client to see the tables in your fresh WordPress installation, the structure of those tables, and the content that they have.  If your memory fails you, here is a quick guide to MySQL.

List all tables in the database:

mysql> show tables;
+-----------------------+
| Tables_in_xxxxxxx_com |
+-----------------------+
| wp_comments           |
| wp_links              |
| wp_options            |
| wp_postmeta           |
| wp_posts              |
| wp_term_relationships |
| wp_term_taxonomy      |
| wp_terms              |
| wp_usermeta           |
| wp_users              |
+-----------------------+
10 rows in set (0.00 sec)

Show structure of the table:

mysql> explain wp_postmeta;
+------------+---------------------+------+-----+---------+----------------+
| Field      | Type                | Null | Key | Default | Extra          |
+------------+---------------------+------+-----+---------+----------------+
| meta_id    | bigint(20) unsigned | NO   | PRI | NULL    | auto_increment |
| post_id    | bigint(20) unsigned | NO   | MUL | 0       |                |
| meta_key   | varchar(255)        | YES  | MUL | NULL    |                |
| meta_value | longtext            | YES  |     | NULL    |                |
+------------+---------------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)

Select all records from the table:

mysql> select * from wp_terms;
+---------+---------------+---------------+------------+
| term_id | name          | slug          | term_group |
+---------+---------------+---------------+------------+
|       1 | Uncategorized | uncategorized |          0 |
|       2 | Blogroll      | blogroll      |          0 |
+---------+---------------+---------------+------------+
2 rows in set (0.00 sec)

Select a single record with a lot of fields for a close-up look:

mysql> select * from wp_links where link_id = 1\G
*************************** 1. row ***************************
         link_id: 1
        link_url: http://codex.wordpress.org/
       link_name: Documentation
      link_image:
     link_target:
link_description:
    link_visible: Y
      link_owner: 1
     link_rating: 0
    link_updated: 0000-00-00 00:00:00
        link_rel:
      link_notes:
        link_rss:
1 row in set (0.00 sec)

While you have just installed a brand new WordPress, you’ll notice that there is already a bunch of data in the database. That data consists of your configuration options, one post, one page, one comment, and a few blogroll links. You might not need all those for your other blogs, but now all of that plays an important role – it shows you were things are and how they are related.

Look into every table. Then change something. Edit a post or add another comment. Tag something. But don’t do everything at once – one step at a time. Check the data in the database after each step, and see how is it different from what you had there before you made a step.

10 minutes later you’ll know everything there is to know about WordPress database structure. Enjoy!