WordPress Plugin Creation “Saving Options”

April 13th, 2009 - Ryan Christenson

Most WordPress plugins, if not all, have options (also known as settings) that are stored in the blog’s database, ready to be called upon when needed. In this tutorial you will explore three different PHP methods to store options.

Method One – Using Variables

The first and most common method is to save each option separately. We use the $_POST variable to save form data in a variable. The variable is then used to update an option.

To save the option:

< ?php
$variable = $_POST['Opt01'];
update_option(‘myPlugin_Opt01′, $variable);
?>

To call the option:

To call the option later on we simply use the get_option function and set the chosen option to a variable.

< ?php
$variable = get_option(‘myPlugin_Opt01′);
?>

Method Two – Arrays and the Extract Function

The first method is easy enough to implement, but can be very bothersome and less efficient for some plugins. If your plugin requires more than ten options, then it’s a good idea to go with a “cleaner” approach, which is where our second method comes in. We will be using arrays and the extract function in this method.

To save your options in an array:

First the options will be saved into an array. This is similar to the first method with the difference being that our variable is now an array.

< ?php
$variable = array(‘opt01′ => $_POST['Opt01'], ‘opt01′ => $_POST['Opt01']);
update_option(‘myPlugin_Opt01′, $variable);
?>

To call the option:

Just as in method one we will be using the get_option function. Now here is where the extract function comes into play.

< ?php
extract(get_option(‘myPlugin_Opt01′);
?>

The extract function makes every key in the array into its own variable. The variables available from the array in this tutorial would be: $opt01 and $opt02.

To check that your option is an array: (ternary style)

It is best to check the option’s contents before extraction. If it is empty, or does not contain an array, an error will occur. In PHP, you can always check if the option is empty or if it is an array.

< ?php
!is_array(get_option(‘myPlugin_Opt01′)) ? “” : extract(get_option(‘myPlugin_Opt01′));
?>

Method Three Arrays and Serialization

Your plugin may require arrays to be recreated. If you’re asking, “Why would I need my arrays recreated?”, there could be a number of reasons; you may need to search through an array, or you may also need to add a new key to a saved array. If your plugin requires the need for arrays to be recreated then serialization is the way to go.

To save the option:

Here we save the variable as an array, just as we did in method two. Then we use the PHP serialize function when we update the option.

< ?php
$variable = array(‘opt01′ => $_POST['Opt01'], ‘opt01′ => $_POST['Opt01']);
update_option(‘myPlugin_Opt01′, serialize($variable));
?>

To call the option:

When calling the serialized array, we first check that the option has data. If it has data, the code will attempt to unserialize the array and assign it to the variable $new. Notice that we don’t check the option with the “is_array” function, since a serialized array will always come up false.

< ?php
get_option(‘myPlugin_Opt01′) == “” ? “” : $new = unserialize(get_option(‘myPlugin_Opt01′));
?>

Conclusion

Any of the three methods discussed in this article can be used in your plugin. To be more efficient with your code, choose a method based on how many options your plugin will require. To get an idea of how many options are necessary, plan out your plugin ahead of time. If you need less than five options, you can go with any method you desire. However, if your plugin requires more than ten options, its best to try method two or three. If you must separate any data in an application, you can always make use of all three methods.

Share on Facebook
 

Leave a Reply




Other Links