The Options API is a way to store data in the WordPress database. The API enables you to add, read, update, and delete options inside the wp_options table. All changes you make using Options API will affect the WordPress admin settings pages of your site.
With Options API, you can change the configuration settings’ options on your WordPress Dashboard. Some people use this method to add custom settings for their plugins or themes.
In this article, I will explain the Options API functions, your wp_options table, and the Settings API, which can help you make changes on your website’s settings pages.
The wp_options Table
The wp_options table stores all of the settings in your WordPress database. Unlike the other tables, the options table doesn’t store the contents of your site. Instead, it’s where the data about your website is stored.
In your wp_options table, you can find information about your WordPress site, such as:
- Your website’s URL
- Admin user email
- Time format
- Posts per page
- Default categories for new posts
- Settings for plugins, themes, and widgets
- Temporarily cached data
The wp_options table stores all of your websites configuration settings in four fields:
- option_id — contains a unique number assigned to every row in the table.
- option_name — shows all the identifying keys of the data.
- option_value — stores the data.
- autoload — displays if the data will be automatically loaded on each page of your WordPress site.
The Options API Functions
To create new options on your website, you can use the Options API. It is one of the simplest ways to manage values in the wp_options table.
There are four functions you can use the Options API for. These functions allow you to add, delete, get, and update your options.
Each function comes in two types — regular and multisite. The regular functions use the wp_options database table, while the multisite functions interact with the wp_sitemeta instead. All options created with the multisite functions will always be autoloaded.
Let’s look at what each of the Options API functions is and how they can modify your database.
These functions below can help you add and delete options on your WordPress website.
With the add function, you can create new options for your wp_options table. The add_option will make changes in the database by using four parameters.
The first parameter is $option. This parameter adds the name of your new setting which becomes the value of the option_name field.
The second parameter is $value. This parameter adds your new option’s value which will be stored in the option_value column of your wp_options table.
The last two parameters are $deprecated and $autoload. For the deprecated parameter, you should leave it as an empty value. For the autoload, the acceptable values are yes or no.
You can use the delete_option function to remove settings from your database table. This function only uses the $option parameter to delete all fields of a specific option.
The add_site_option adds a new option to the wp_sitemeta table instead of the wp_options. It requires $option and $value parameters, which use the same values as in the add add_option function.
To remove multisite options, we use delete_site_option options instead of the delete_option function. This function uses one parameter which is the $option name that we want to remove from the wp_sitemeta table.
To retrieve and update options from your database, we can use these four functions below.
You can use get_option to see whether you need to add new options. If the return value is false, then the $option either doesn’t exist or doesn’t have a value.
This function uses two parameters, $option and $default, to get options from the wp_options table.
We use update_option to update the value of an option already in the wp_options table. update_option uses three parameters: $option and $new_value, which are required, as well as $autoload, which is optional.
$option – is the name of the item that you want to update.
$new_value – is the content that you want to set to the existing option.
$autoload – is a parameter that can only be updated if $value is also changed.
The get_site_option function can retrieve options from the wp_sitemeta table. It uses $option and $default parameters, just like the get_option function.
We use update_site_option to update the values of options in the WordPress multisite. This function is similar to the update_option, except it doesn’t use the $autoload as one of the parameters.
The Settings API
As one of the most popular WordPress APIs, the Settings API lets you create new settings, fields, and group fields on your admin setting user interface. Some people use the Settings API to add custom options for their plugins or themes.
Though Options API can help you easily manage options on your database, it doesn’t provide a way to add an option to your user interface on your settings page. Therefore, WordPress created the Settings API and added it to WordPress 2.7.
Using the Settings API is an alternative way to change the settings interface. Web developers can do this task without using this API. But, there are several benefits to using the Settings API:
- Visual consistency — your custom options page will look like the other administrative content.
- Robustness — using this API will make your custom settings page be recognized as part of WordPress Core. As a result, your settings will be more stable, and updates won’t affect your customization.
- Ease of use — the WordPress API provides built-in support that helps to retrieve and store your options data.
- Security measures — WordPress will take care of all settings security checks.
With the Settings API, you’ll have the capabilities to modify the settings page with ease. All you need to do is register your options, register_setting()decide the input fields that you want to add to your custom options page, $option_group, $option_name, $sanitize_callback, and your options page is ready to use.
It is just as easy to undo your settings from the wp_options table. Simply deregister, unregister_settings() and use the same fields you used while registering. ($option_group, $option_name, $sanitize_callback)
With the Options API, you can manage the option data inside the wp_options table with ease.
The wp_options table is the place to store all configuration settings and other information about your website. To interact with this table, you can use the Options API functions or the Settings API.
By using the Options API functions, you can add, delete, get, and update the option items in your options table.
The Settings API is an interface API that can help you create a custom settings page on your site. Using this feature gives you several benefits, such as visual consistency, robustness, ease of use, and security.
Now you know what the Options API and its relation with the wp_options table is. I hope this article helps you understand how you can manage the settings options of your website.