Options API

Advertisement

The complete guide of WordPress Options API with examples.

Table of Content

Introduction Introduction

In WordPress, we have an options table that helps us to save the values into the options table.

To store the values in option table WordPress use the database table wp_options

Or

If you have change the database table prefix of your WordPress setup then the option is stored in $wpdb->prefix}_options

Check the database table prefix Check the database table prefix

Open the wp-config.php file and find the variable $table_prefix. You’ll see something like below:

/**
 * WordPress Database Table prefix.
 *
 * You can have multiple installations in one database if you give each
 * a unique prefix. Only numbers, letters, and underscores please!
 */
$table_prefix  = 'wp_';

By default WordPress use the table prefix wp_ for creating database tables.

Top ↑

Option API Functions Option API Functions

WordPress provide 8 type of functions to work on the Options API. Below is the list of all of them.

  1. add_option()
  2. get_option()
  3. update_option()
  4. delete_option()
  5. add_site_option()
  6. get_site_option()
  7. update_site_option()
  8. delete_site_option()

We can use them on simple site and some of them on network/multisite.

Lets see all the available functions in WordPress Options API.

Top ↑

add_option() add_option()

The function add_option() is pretty simple to use. We can store number, string, array etc into the options table.

Syntax:

add_option( <option-key> );

Let’s see few examples.

Example: Add String value into the options table Example: Add String value into the options table

add_option( 'my_string_option', 'Hello World' );

Here, our option name is my_string_option and we have store the value Hello World within it.

We’ll use function get_option() to get the stored value of our option. We’ll see it in the next section.

Let’s see some other examples.

Top ↑

Example: Add Number value into the options table Example: Add Number value into the options table

add_option( 'my_number_option', 299 );

Here, our option name is my_number_option and we have store the value 299 within it.

Top ↑

Example: Add Boolean value into the options table Example: Add Boolean value into the options table

add_option( 'my_boolean_option', true );

Here, our option name is my_boolean_option and we have store the value true within it.

Top ↑

Example: Add Array into the options table Example: Add Array into the options table

// PHP array.
$data = array( 'one', 'two', 'three' );
// Add into the option.
add_option( 'my_array_option', $data );

Here, our option name is my_array_option and we have store the value array( ‘one’, ‘two’, ‘three’ ) within it.

Top ↑

Example: Add Multidimensional Array into the options table Example: Add Multidimensional Array into the options table

// PHP Multidimensional array.
$data = array(
     'one' => 'One',
     'two' => 'Two',
     'three' => 'Three'
);
// Add into the option.
add_option( 'my_multidimensional_array_option', $data );

Here, our option name is my_multidimensional_array_option and we have store the value array( ‘one’ => ‘One’, ‘two’ => ‘Two’, ‘three’ => ‘Three’ ) within it.

Top ↑

get_option() get_option()

We get the stored option value with the help of get_option().

Syntax

get_option( <option-key>, <default-value>

Here, We can pass the option key and the default value too.

Example

$value = get_option( 'my_option', 'Initial Value' );
var_dump( $value );
// Initial Value

Here, we have get the value of option my_option. If the option my_option value is not set then it get the default value.

In our above example the option value is not set for that get_option() return the Initial Value as option value.

Let’s see our above examples in which we have store string, number, boolean and array values.

Top ↑

Example: Get String value from the options table Example: Get String value from the options table

$value = get_option( 'my_string_option', 'Default value' );
var_dump( $value );
// string(11) "Hello World"

Here, we have store the value Hello World within option my_string_option is it returns the value Hello World.

If option my_string_option is not defined before then we get “Default value“.

Top ↑

Example: Get Number value from the options table Example: Get Number value from the options table

$value = get_option( 'my_number_option' );
var_dump( $value );
// string(3) "299"

Here, Our value 299 is saved as a string. So, While getting the value from the options table then make sure to use the int(), absint() functions.

Getting value with int() function:

$value = (int) get_option( 'my_number_option' );
var_dump( $value );
// int(299)

Getting value with absint() function:

$value = absint( get_option( 'my_number_option' ) );
var_dump( $value );
// int(299)

Top ↑

Example: Get Boolean value from the options table Example: Get Boolean value from the options table

$value = get_option( 'my_boolean_option' );
var_dump( $value );
// string(1) "1"

Same as a number value when we get the boolean value then we get the true value as string 1.

We can use the function boolval() to get the stored value as boolean.

$value = boolval( get_option( 'my_boolean_option' ) );
var_dump( $value );
// bool(true)

Top ↑

Example: Get Array values from the options table Example: Get Array values from the options table

$data = get_option( 'my_array_option' );
var_dump( $data );
// array(3) { [0]=> string(3) "one" [1]=> string(3) "two" [2]=> string(5) "three" }

We get our stored array values. We can iterate array values with foreach loop as below:

foreach ($data as $key => $value) {
	echo $key . ' ' . $value . '<br/>';
}
// 0 one
// 1 two
// 2 three

Top ↑

Example: Get Multidimensional Array values from the options table Example: Get Multidimensional Array values from the options table

$data = get_option( 'my_multidimensional_array_option' );
var_dump( $data );
// array(3) { ["one"]=> string(3) "One" ["two"]=> string(3) "Two" ["three"]=> string(5) "Three" }

Same as an array we get our stored multidimensional array values. We can iterate array values with foreach loop as below:

foreach ($data as $key => $value) {
	echo $key . ' ' . $value . '<br/>';
}
// one One
// two Two
// three Three

Top ↑

update_option() update_option()

We can update the values of existing option with the help of update_option().

NOTE: that when we use update_option() to update the value of any option and the option is not exit the it use add that option with function add_option().

Syntax:

update_option( <option-key>, <new-value> );

E.g.

update_option( 'my_option', 'My Option Value' );

Here the value My Option Value is updated in option my_option. And if my_option is NOT exist then it is new option created with value My Option Value.

Top ↑

Update option examples Update option examples

Let’s see all our above example in single code snippet as below:

// Update String value.
update_option( 'my_string_option', 'New String Goes Here..' );
// Update Number value.
update_option( 'my_number_option', 8000 );
// Update Boolean value.
update_option( 'my_boolean_option', false );
// Update Array values.
$data = array( 'eleven', 'twelve', 'thirteen' );
update_option( 'my_array_option', $data );
// Update Multidimensional Array values.
$data = array( 'eleven' => 'Eleven', 'twelve' => 'Twelve', 'thirteen' => 'Thirteen' );
update_option( 'my_multidimensional_array_option', $data );

Here, We have updated all the old values with new values.

Top ↑

delete_option() delete_option()

We can delete the option with the help of option key.

Syntax

delete_option( <option-key> );

To delete the option we need to pass the option key within the delete_option() function.

Top ↑

Delete option examples Delete option examples

Let’s see how to delete all our recently added options.

// Delete String option.
delete_option( 'my_string_option' );
// Delete Number option.
delete_option( 'my_number_option' );
// Delete Boolean option.
delete_option( 'my_boolean_option' );
// Delete Array option.
delete_option( 'my_array_option' );
// Delete Multidimensional Array option.
delete_option( 'my_multidimensional_array_option' );

Top ↑

add_network_option() add_network_option()

Same as add_option() we can use add_network_option() on network/multisite .

Syntax:

add_network_option( <network-id>, <option-key>, <option-value> );

Here,

  • <network-id> – Network Site ID.
  • <option-key> – Option key.
  • <option-value> – Option value.

Top ↑

Add network option examples Add network option examples

Same as add_option() we can store any values on network site with add_network_option() so we can sonsider add_option() for add_network_option().

Top ↑

get_network_option() get_network_option()

Same as get_option() we can use get_network_option() on network/multisite.

Syntax:

get_network_option( <network-id>, <option-key>, <default-value> );

Here,

  • <network-id> – Network Site ID.
  • <option-key> – Option key.
  • <default-value> – Option default value.

Top ↑

Get network option examples Get network option examples

Same as get_option() we can get any values from network site with get_network_option(). So we can consider the get_option() examples for get_network_option().

Top ↑

update_network_option() update_network_option()

Same as update_option() we can use update_network_option() on network/multisite.

Syntax:

update_network_option( <network-id>, <option-key>, <option-value> );

Here,

  • <network-id> – Network Site ID.
  • <option-key> – Option key.
  • <update-value> – Option update value.

Top ↑

Update network option examples Update network option examples

Same as update_option() we can get any values from network site with get_network_option() so we can consider the get_option() examples for get_network_option().

Top ↑

delete_network_option() delete_network_option()

Same as delete_option() we can use delete_network_option() on network/multisite.

Syntax:

delete_network_option( <network-id>, <option-key> );

Here,

  • <network-id> – Network Site ID.
  • <option-key> – Option key.

Top ↑

Delete network option examples Delete network option examples

Same as delete_option() we can get any values from network site with delete_network_option() so we can consider the delete_option() examples for delete_network_option().

Complete Guide of WordPress Options API with examples.

Leave a Reply