What are Plugin add_option and Update_option

Every one and more plugins feature a settings page included in them which help users configure that plugin to respond an perform in different ways without actually modifying any code behind the plugin by saving various option settings using add_options and update options.

The first step in this process is saving and retrieving options in WordPress, lets see how to save options with WordPress plugin.

Saving Plugin Options in WordPress Plugin Settings

When a developer writing or structuring building a plugin it’s important to save some options for your plugin which you will need to write backend code.

WordPress offers with easy features some very easy-to-use functions to save, edit, and delete options, you will find more options to play around with saving plugin options in WordPress codex for developers official website.

2 Functions are Available to Create Options for WordPress Plugin Settings

The most important and most used 2 functions are already available for creating options:

1. add_option()

2. update_option().

These 2 functions in WordPress create options, but update_option() also updates the option if it already exists. Here’s an example of adding a new option:

adding-a-new-option
adding-a-new-option

First Parameter to Send add options for WordPress Plugin Settings

The first parameter you should send to direct the code to the add_option() function will be the name of your option. This is a mandatory or required field and must be unique from all other options which are saved in WordPress, including from other plugins.

Second Parameter to Send Update option () in WordPress Plugin

The second parameter is the option value. This is also a required field and can be a string, an array, an object, or a serialized value. You can also use update_option() to create new options and what this function do is it checks whether the option exists first and if not created it.

If, however, the option already exists, it will updates the value with the new option value you are sending in. You call the update_option() function exactly as you did when adding an option like so:

Related Coverage:

1. Template Tags in WordPress and Uses

2. How to Use WP_Query Object in WordPress with WP Query Custom Loops

3. WordPress Debug log: Enable in wp-config.php, SET to True & SET Log path file

calling-update-option
calling-update-option

Generally, the update_option() function is used for both adding and updating options in plugins. It’s much easier to stay consistent with one function call for both rather than calls to different functions for adding and updating your plugin options. Retrieving an option value is just as easy. To retrieve any option, use the get_option() function, as shown here:

echo-get-options
echo-get-options

The only required field for get_option() is the name of the option you want to retrieve. If the option exists, it is returned to display or it is stored in a variable. If the option doesn’t exist, the function will return FALSE.

Options can be deleted as easily as they are created. To delete an option, use the delete_option() function. The only parameter is the option name that you want to delete:

deleting-option-wordpress
deleting-option-wordpress

If you have a number of options, it is a smart idea to store them in an array. This also makes it much easier to follow your code logic when there is a set naming convention used on variables, functions, and so on. Options in WordPress are not reserved for just plugins.

Themes can also create options to store specific theme data. Many of the themes available today offer a settings page, enabling you to customize the theme through settings moderately than code.

Ramana
Ramanahttps://www.asavvyweb.com
Ramana Tula is a Google Product Expert - He is a Full stack Web and Android Developer also - SEO Manager and also manages Digital Marketing.

- Advertisement -