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. 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:
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:
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() for WordPress Plugin Settings
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()
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:
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:
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.