Plugin Settings in WordPress

If you use Plugins then you already know that mostly plugins have settings page. Plugin setting page helps the user to modifying the plugin as user demand without actually modifying the code behind the plugin by saving various option settings. For this purpose the first step in this process is saving and retrieving options in WordPress.

Saving Plugin Options in WordPress by Web Design London

During the developing of Plugin may be some chances are that you will need to save some options for your plugin. So for this purpose in WordPress has some features that is very easy-to-use functions to save, edit, and delete the options. There are two functions that are available in wordpress for creating options: add_option() and update_option(). These both functions are create options, but the update_option() perform both functionalities create and updates. Below is an example of adding a new option:

<?php add_option( ‘wspo_display_mode’, ‘Web Design London’ ); ?>

Above function the add_option()first parameter is the name of your option. Remember that this is a required field and it must be unique from all other options saved in WordPress, other plugins including also. Second parameter is the option value. It can be a string or an array or an object value it is also required field. As tell you before that you can also use update_option() to create new options. The update_option() Will check that whether the option exists first or not, if it not creat than it will create otherwise it updates the value with the new option value you are sending in. So this is the same like add_option() function and you call the update_option() function exactly like so:

<?php update_option( ‘wspo_display_mode’, ‘ Web Design London ‘ ); ?>

As previously discussed that the update_option() function generally is used for both adding and updating options in plugins. So this is very easy to use this function for both rather than calls to different functions for updating and adding your plugin options. For retrieving an option value is just as easy in WordPress. For retrieve any option, the get_option() function use, you can see here:

<?php echo get_option( ‘wspo_display_mode’ ); ?>

It is only one required parameter by the get_option() function is the name of the option you want to retrieve. This option work like Boolean variable, it will returned true if the option exist, If the option doesn’t exist, the function returns FALSE.

It is very easy to deleted an option. For delete an option use the delete_option() function. It is only one parameter require for delete:

<?php delete_option( ‘wspo_display_mode’ ); ?>

Good practice of coding is that start all of your option names with the same prefix, like wspo_ in the above examples. Using prefix good for couple of reasons first it uniqueness and readability.

I think you have understood about the saving options in WordPress, you should to remember also that options in WordPress are not reserved for just plugins use. For the purpose of theme it can also create options to store specific theme data in wordpress. There are many of the themes are available that offer a settings page, by this page you can enabling to customize the theme through settings rather than code.