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;
