21 April 2014
Wordpress provides a set of functions that enable easy access to plugin options. By default wordpress and plugins options are stored in wp_options
table. Let’s explore these functions.
You can add plugin options using add_option
function. It accepts four parameters:
'no'
can convert this and the option will not be loaded during loading.Let’s start by saving your first plugin options.
//always put your plugin options in array
add_option('myplugin_settings', array(
'option1'=>'value',
'option2'=>'value'
));
//another option but disable autoload
add_option('myplugin_another_options','value','','no');
To get option value, you can use get_option
function. It accepts only one parameter and it is the option name. This function returns false
if option doesn’t exist.
$pluginsettings = get_option('myplugin_settings');
$another_settings = get_option('myplugin_another_options');
To update an option, you can use update_option
function. It accepts two parameters. The first parameter is the option name and the second parameter is the new value. This function creates option if it doesn’t exist.
update_option('myplugin_settings',array(
'option1'=>'updated value',
'option2'=>'updated value'
));
To delete option, you can use delete_option
function. It accepts a string representing option name. It returns false
if option doesn’t exist and true
otherwise.
delete_option('myplugin_settings');
delete_option('myplugin_another_options');