The WordPress Option API is a database storage mechanism provided to developers, which can be quickly and safely stored in a database (both in the Wp_options table) by invoking functions.
The pattern for each setting is Key–value, which facilitates expansion. The Option API is not just for themes and plugin developers, but many of the WordPress settings are stored here, such as the site name.
Get settings
Getting settings is a very common operation, mainly using the get_option () function:
1 |
Get_option ($option, $default); |
Parameters:
$option
(string) (must) the key name of the setting to get.
Default value: None
$default
(mixed) (optional) What data is returned when the specified key value is not found in the database.
Default value: False
This function should be very common, such as getting the name of a blog:
1 |
$blogname = get_option (' name '); |
Add settings
To add a setting with the add_option () function, use:
1 |
Add_option ($option, $value, $deprecated, $autoload); |
Parameters:
$option
(string) (must) the key name of the setting to be added, cannot be duplicated, the setting already exists is not added.
Default value: None
$value
(mixed) (optional) The value to store, which can be any data type or even Object.
Default value: Empty string
$deprecated
(string) (optional) historical legacy variable, now useless, do not set or empty the string.
Default value: Empty string
$autoload
(string) (optional) Whether to add this setting to the cache, fill in yes or No.
Default value: Yes
Example:
123456 |
Add_option (' example_option ', ' Save a word ');//Save String Add_option (' Example_option2 ', true);//Save Boolean add_option (' Example_ Option3 ', Array (' A ', ' B ', ' C ', ' d ', 5, 6, True, false));//Save the Array add_option (' Example_option4 ', 12);//Save integer add_option (' ex Ample_option5 ', 4.4123);//Save floating-point add_option (' Example_option6 ', new example_obj);//Save Objects (object) |
Note that this function has an important feature, that is, if the setting of the specified key name already exists, it will not continue to be added.
Update settings
The update setting uses the update_option () function, which is added or updated regardless of whether the settings are present.
Parameters:
$option
(string) (must) the key name of the setting to update.
Default value: None
$newvalue
(mixed) (must) the value to update, which can be any data type, even Object.
Default value: None
Example:
123456 |
Update_option (' example_option ', ' Update a Session 2 ');//Update the string update_option (' Example_option2 ', false);//Update the Boolean value Update_option (' Example_option3 ', Array (' d ', ' C ', ' B ', ' a '));//update array update_option (' Example_option4 ',-11);//update integer update_option (' Examp Le_option5 ', 1.3123);//update floating-point update_option (' Example_option6 ', new example_obj2);//Update Objects (object) |
Delete Settings
Delete a setting with the delete_option () function, and you can delete it by specifying the key value directly.
Parameters:
$option
(string) (must) the key value of the setting to delete.
Default value: None
Example:
123456 |
Delete_option (' example_option ');d elete_option (' Example_option2 ');d elete_option (' Example_option3 ');d Elete_ Option (' Example_option4 ');d elete_option (' example_option5 ');d elete_option (' example_option6 '); |
Summary
Take advantage of the Option API to easily and securely save data to your data, eliminating the hassle of SQL statements.
WordPress Option API (database storage API)