In the earlier parts of this series, we looked at the tables in the WordPress database and the relationships between them.
In this part I'll cover a table which is different from the others - the
wp_options table. As you can see from the diagram below, this is the only table which sits on its own:
The options table stores a different kind of data from the other tables: instead of storing data about your site's content, it stores data about the site itself. Data is written to the options table using the Options API or the Settings API, both of which consist of a set of functions used to add, update and delete data from this table.
You can add values to existing options and you can also add new records to the table when you want to create new options.
In this tutorial I'll look at different aspects of the options table and how you interact with it:
- Access to the
- Structure of the
- Populating the
- The Options API
- The Settings API
I will just give an overview of the APIs and how they interact with the options table - if you want to learn more, read Tom McFarlin's series on the Settings API.
Access to the wp_options Table
wp_options table stores data which is related to the setup and administration of the site as a whole, access to it is restricted. To be able to amend settings and options, users will need to have the
manage_options capability. The only default user role with this capability is the administrator role (and in Multisite, the network administrator role).
This means that if you need to add options that other user roles have access to, you'll have to assign the
manage_options capability to them. This carries risks, so only do it if you're sure!
Structure of the wp_options Table
The options table has a similar structure to the three metadata tables. It has four fields:
autoload- specifies whether the option is automatically loaded on each page load - defaults to
yesin a single site installation and
Each record in the
option_name field will be a unique value: if you add more than one value to an option, WordPress stores this in an array in the
option_value field. A good example of this is the
active_plugins option, which stores an array of plugins activated on your site.
When adding, editing or deleting data in the
wp_options table, you must always specify the
option_name, as I'll show later in this tutorial.
Populating the wp_options table
wp_options table is populated from one of three sources:
- the default Settings screens
- theme options screens
- settings and options screens which you add via plugins
There are a number of options built in to WordPress - you can see all of these in the Option Reference. But you can also create your own.
To create new options in your theme or plugin, you would use the Options API or the Settings API. I'll cover these in more detail below.
Using the Options API
The Options API consists of eight functions which allow you to add, get, update or delete options:
|Deletes all fields for that option|
|Similar to |
|The same as |
|Similar to |
|Identical to |
Note that when creating options, either via the Options API or the Settings API, you can create records with no value in the
option_value field. This allows site administrators to populate that field at a later time.
Using the Settings API
As well as the Options API, you can also use the Settings API to interact with data in the
wp_options table. The Settings API lets you create settings which site administrators can use to add or update data in the options table - it adds a user interface to your options.
The Settings API has more to it than the Options API so I won't cover it in detail here, but essentially it has three elements:
- the setting (the data in the
- the field (which is used to add and edit data)
- the settings section, which is a group of related fields.
The two functions in the Settings API which interact directly with the
wp_options table are as follows:
|Deregisters settings from the |
These functions don't add values to the options in the
wp_options table, but they create settings which can than have values added to them via other functions in the Settings API.
wp_options table is unique among WordPress database tables in that it doesn't share a relationship with any of the other tables. This is because it stores data about the site or network, and not the content. To interact with the options table, you can use the functions in the Options API or the Settings API, and you can also make use of functions which add data network-wide in a Multisite insulation.
In the final part of this series I'll look at Multisite, as it uses some additional database tables which have not been covered so far in this series, and also creates multiple instances of each of the core tables, one for each site.