Skip to content

Database options ​

The Options service is used to interact with the WordPress options API. The service provides methods to get, set, update, and delete options.

The service saves all options in the wp_options table of the WordPress database, under one row, using the slug of the plugin as the prefix for the option name. If your plugin slug is my_plugin, the option name will be my_plugin_options.

TIP

The service is automatically registered with the container and can be accessed by injecting it into your classes. The service is an instance of the Options class.

Getting an option ​

To get an option, you can use the get_option() method of the Options service. The method accepts 2 parameters:

  1. The name of the option.
  2. The default value to return if the option does not exist.
php
$option = $options->get_option('my_option', 'default_value');

Setting an option ​

To set an option, you can use the update_option() method of the Options service. The method accepts 2 parameters:

  1. The name of the option.
  2. The value of the option.
php
$options->update_option('my_option', 'option_value');

Deleting an option ​

To delete an option, you can use the delete_option() method of the Options service. The method accepts 1 parameter:

  1. The name of the option.
php
$options->delete_option('my_option');

Getting all options ​

To get all options, you can use the get_options() method of the Options service. The method returns an array of all options.

php
$options->get_options();

Reference documentation ​