WordPress Option API (database storage API)

Source: Internet
Author: User
Tags autoload deprecated

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.