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
wp_options
table - Structure of the
wp_options
table - Populating the
wp_options
table - The Options API
- The Settings API
Access to the wp_options Table
As thewp_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:option_ID
option_name
option_value
autoload
- specifies whether the option is automatically loaded on each page load - defaults toyes
in a single site installation andno
in Multisite.
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
Thewp_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
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:Function | Parameters | Notes |
---|---|---|
add_option() | $option , $value , $deprecated , $autoload | Only $option is required. If there's an existing record with your $option parameter as the value of its option_name field, WordPress will add your $value to an array in the option_value field for that record, otherwise it will create a new record. |
delete_option() | $option | Deletes all fields for that option |
get_option() | $option , $default | $default (optional) is the default value to return if no value is stored against the option in the database. |
update_option() | $option , $new_value | $new_value is the value which will populate the option_value field |
add_site_option() | $option , $value | Similar to add_site_option() but adds the option network-wide in Multisite (meaning that the option is stored in the wp_options table and not the wp_XX_options table where XX is the site ID). $autoload is not included as site options do not autoload in Multisite and this cannot be overridden. |
delete_site_option() | $option | The same as delete_option() but works network-wide in Multisite. |
get_site_option() | $option , $default , $use_cache | Similar to get_option() but retrieves the network-wide option in Multisite. |
update_site_option() | $option , $value | Identical to update_option() but works network-wide in Multisite. |
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 thewp_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
wp_options
table) - the field (which is used to add and edit data)
- the settings section, which is a group of related fields.
wp_options
table are as follows:Function | Parameters | Notes |
---|---|---|
register_setting() | $option_group , $option_name , $sanitize_callback | The $option_name parameter refers to the option_name field in the wp_options table; the other parameters interact with other functions in the Settings API |
unregister_setting() | $option_group , $option_name , $sanitize_callback | Deregisters settings from the wp_options table - normally used with deactivation hooks for themes or plugins. |
wp_options
table, but they create settings which can than have values added to them via other functions in the Settings API.Summary
Thewp_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.