How to Save Options to Database in WordPress

How to Save Options to Database in WordPress

One of the simplest ways to store data in WP is to use the database table called “wp_options”. Typically, this table is used to store plugin or theme data that is not related to meta values (such as for posts, users, or taxonomies).

WordPress provides a set of built-in functions and filters to interact with this table.

Let’s take a look at a small working example:

$option_key = 'my_option_var';

echo "<b>get_option</b>";
$result = get_option($option_key);
var_dump($result);
echo "<hr>";

echo "<b>get_option + default value</b>";
$result = get_option($option_key, 'default_value');
var_dump($result);
echo "<hr>";

echo "<b>add_option with string</b>";
$result = add_option($option_key, 'option_value', '', false);
var_dump($result);
echo "<hr>";

echo "<b>get_option</b>";
$result = get_option($option_key);
var_dump($result);
echo "<hr>";

echo "<b>update_option with array</b>";
$result = update_option($option_key, array(
	1 => 'One',
	'Two' => 3,
	'Three' => 'Three',
));
var_dump($result);
echo "<hr>";

echo "<b>get_option</b>";
$result = get_option($option_key);
var_dump($result);
echo "<hr>";

echo "<b>delete_option</b>
";
$result = delete_option($option_key);
var_dump($result);
echo "<hr>";
die;

In this part of the code:

$option_key = 'my_option_var';

echo "<b>get_option</b>";
$result = get_option($option_key);
var_dump($result);
echo "<hr>";

we try to retrieve an option named “my_option_var”. If the value was previously set, we will get it. Otherwise, the function will return false.

We can also set a default value:

echo "<b>get_option + default value</b>";
$result = get_option($option_key, 'default_value');
var_dump($result);
echo "<hr>";

and it will be returned by the function if no matching entry is found in the DB.

To set a value, we can use the following:

echo "<b>add_option with string</b>";
$result = add_option($option_key, 'option_value', '', 'yes');
var_dump($result);
echo "<hr>";

where:
$option_key — the name of the option
option_value — the content of the option
the third empty parameter — is no longer used
'yes' — defines whether the option will be autoloaded on site load, or only when manually requested. The alternative is “no”. You can also use true/false.

WordPress also allows storing arrays in this variable:

echo "<b>update_option with array</b>";
$result = update_option($option_key, array(
	1 => 'One',
	'Two' => 3,
	'Three' => 'Three',
));

These are automatically serialized before storage. When retrieved, the string is automatically unserialized into an array.

To delete an option, use the “delete_option” function:

echo "<b>delete_option</b>";
$result = delete_option($option_key);
var_dump($result);
echo "<hr>";

If the deletion is successful, true will be returned.
That’s it.

Posts on similar topics

Are you having problems with your WordPress site? Do you need additional functionality? A custom plugin or a new page?
Then write to me via the feedback form, and I will try to help you.

Write a comment

Your email address will not be published. Required fields are marked *