WordPress provides a number of functions to make it easier for developers to store and retrieve data from the database. One of these functions is add_option
.
The add_option
function is used to add a new option to the WordPress options table. Options are used to store site-wide settings, such as the site title, site URL, or the number of posts to show on the homepage.
To use add_option
, you need to provide it with two arguments: the name of the option you want to add, and the value you want to set for that option. The function will then add the option and its value to the options table.
Here is an example of how to use add_option
to add a new option to store the site’s Twitter handle:
add_option( 'site_twitter_handle', 'example_twitter' );
In this example, the first argument is the name of the option (site_twitter_handle
), and the second argument is the value we want to set it to (example_twitter
).
Once the option is added, you can retrieve its value using the get_option
function. For example:
$twitter_handle = get_option( 'site_twitter_handle' );
This will retrieve the value of the site_twitter_handle
option and store it in the $twitter_handle
variable.