At first, let’s take a look what kind of data is stored in wp_options table.
Follow this link to view all data saved in wp_options table.
http://example.com/wp-admin/options.php
The URL may be of your website or your local machine. Both of them can be viewed here:
/wp-admin/options.php

wp_options Table Data

You can directly change any data from here [Which are editable here].
Now lets see, how we can save our plugins data in wp_options table. And also how we can show them in front end.
add_option is used to add new options. First parameter is option name and second parameter is options default value. add_option accepts 4 parameters. Only first parameter (option name) is required.


function working_with_wp_options()
{
    $option_name = add_option('delowar_me_settings', 'Here is my wp_options table data');
}
add_action("delowar_me_wp_options","working_with_wp_options");

This will result in following results in
/wp-admin/options.php

Data added to  wp_opitons table
option data in database (phpmyadmin)

Now lets read the data and use it in our theme and plugin. For simplification, let’s just simply show it on a page.
In our view page let’s add the action name like below:
do_action("delowar_me_wp_options");

Add the code in your view page for example in page.php or single.php etc.

Now in theme or plugin functions lets add the following code:


function reading_wp_options_data()
{
    $option_data = get_option('delowar_me_settings');
    echo $option_data;
}
add_action("delowar_me_wp_options","reading_wp_options_data");

The above code will result in the following result in your view page.

getting and viewing data on front end

Now for updating use update_option


function updating_wp_options()
{
    $update_data = update_option('delowar_me_settings', 'Here is my UPDATED wp_options table data');
}
add_action("delowar_me_wp_options","updating_wp_options");


This will update the value of delowar_me_settings in wp_options table.

We may need to delete our plugin settings data when user uninstall our themes or plugin. One of the basic and easiest way to do this is given below.


function deleting_wp_options_data()
{
    $update_data = delete_option('delowar_me_settings', 'Here is my UPDATED wp_options table data');
}
add_action("delowar_me_wp_options","deleting_wp_options_data");

This will delete both our option name and option data.


Leave a Reply

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