WordPress Options API
The Options API is a core WordPress feature that allows developers to store, retrieve, and manage site-wide settings in the database. It’s commonly used to save plugin, theme, or general configuration data that applies across the entire site.
Where Are Options Stored?
All options are saved in the wp_options
table in the WordPress database as key-value pairs. They are autoloaded by default, meaning they are loaded on every page load unless specified otherwise.
Common Functions
Function | Description |
---|---|
add_option() | Add a new option to the database |
get_option() | Retrieve the value of an option |
update_option() | Update an existing option value |
delete_option() | Remove an option from the database |
Example Usage
// Save a value
add_option( 'my_custom_setting', 'enabled' );
// Retrieve it
$value = get_option( 'my_custom_setting' );
// Update it
update_option( 'my_custom_setting', 'disabled' );
// Delete it
delete_option( 'my_custom_setting' );
Autoloading Options
By default, options are autoloaded with every page request. You can disable this when adding an option:
add_option( 'heavy_data', $data, '', 'no' );
Use this for large or rarely-used options to avoid performance issues.
Typical Use Cases
- Plugin settings
- Theme options
- API credentials
- Feature toggles
- Site-wide preferences
Best Practices
- Use unique option names (preferably prefixed with your plugin/theme name)
- Store simple values or serialized arrays, not entire objects or heavy data
- Be mindful of autoloaded options — keep them minimal