WordPress Database – How Sidebars Work

Widget areas, sidebars and widgets themselves are all stored in the database. Manipulating them from the widget editor works like magic, but in the background values in the options table are continuously changed. In this article we’ll look at exactly how widget areas and widgets are handled on a database level in WordPress.

The post WordPress Database – How Sidebars Work appeared first on Kinsta Managed WordPress Hosting.

Spread the love

Sidebars are an integral part of the WordPress experience but understanding how they work on a database-level is not at the forefront of tutorials. You can find any number of posts about creating widgets, customizing them, extending them and so on, but how they are actually put together at the end and come together in multiple sidebars is often overlooked.

In this article – true to the database series – we’ll be looking at how sidebars and widgets are stored in the database.

The term sidebar is actually a misnomer since that’s not really what we’re talking about. The term sidebar comes from a time when sidebars became prevalent and they were the first to be “widgetized”. Since then we have found that footers, headers and other sections can sometimes benefit from widgets – modular content blocks.

From here on out I will be referring to “widget areas”. A sidebar can be a widget area, just like any other section on your website can potentially contain widgets. In a nutshell: any area that can contain widgets and can be controlled from the widgets section in the admin constitutes a widget area.

The widget areas – and indeed all widgets – are stored in the options table. The first row to look at will be the one with the option_name of sidebars_widgets. The content of the option_value column contains all the defined sidebars and the ID of the widgets within. If you use var_dump( get_option( 'sidebars_widgets' ) ) you’ll see something like this:

( [wp_inactive_widgets] => Array ( ) [sidebar-1] => Array ( [0] => text-2 [1] => categories-2 ) [array_version] => 3

In my current setup, I don’t have any inactive widgets and I have one sidebar (sidebar-1) with a text widget and a category list widget – the ID of these widgets is shown in the array.

Based on this info we know what sidebars are defined and what widgets they contain but we don’t know anything about the configuration about each specific widget. To get that information we need to look at some further rows.

Each widget has a separate row in the options table. The value of the option contains all defined widgets of the type and their settings. To get all defined text widgets we need to look at the value of the widget_text option. To view category list widgets we need the widget_categories option.

( [1] => Array ( ) [2] => Array ( [title] => First Text Widget
[text][/text] => Fist Content
[filter] =>
) [_multiwidget] => 1

I’ve pasted the results of get_option( 'text_widget' ) above. There is an empty array there which is the remnant of a deleted widget, the second member corresponds to text-2 from the value of sidebars_widgets. It contains all the options for the widget.


We now have all the data required to assemble sidebars of our own accord if needed, and hopefully a deeper understanding of how things work in the background. I think that knowing how sidebars work on the database level gives us insight into manipulating them with the functions available out of the box, and manipulating them in unique ways if the need arises.

Facebook Comments

More Stuff

WordPress 3.1 Released – New Features WordPress 3.1 was just released. Here are the highlights according to the WordPress site, along with some commentary by your truly 😉 Highlights I...
How to Optimize Post-Purchase Behavior in WooCommerce (Data Says) Why encouraging positive post-purchase behavior is something you should look into? Well, for once, there’s so much potential in e-commerce overall. ...
Intro To Podcasting With WordPress This a guest post by my friend and multimedia WordPress wizard, Jason Tucker. He streams live and archives the OC WordPress meetup group, and has rec...
How to Convert Your Website into a Mobile App Using a WordPress Pl... It probably won’t surprise you that more people are using mobile devices than ever before. In fact, your customers likely spend the majority of their ...
Spread the love

Posted by News Monkey