Most WordPress plugins, if not all, have options (also known as settings) that are stored in the blog’s MySQL database, ready to be called upon when needed. In this tutorial we will explore three different PHP methods to store options.
Method One – Using Variables
The first and most commonly used method is to save each option with separate variables.
To save the option:
We use the $_POST variable to save form data in a variable. The variable is then used to update an option.
$variable = $_POST['option01'];
update_option('my-plugin-options', $variable);
To call the option:
To call an option we simply use the get_option function and set the chosen option to a variable.
$variable = get_option('my-plugin-options');
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. This is where our second method comes in, using arrays and the extract function.
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.
$variable = array('option01' => $_POST['option01'], 'option02' => $_POST['option02']);
update_option('my-plugin-options', $variable);
To call the option:
To call the option we will once again be using the get_option function. Now here is where the extract function comes into play.
extract(get_option('my-plugin-options');
The extract function makes every key in the array into its own variable. The variables available from the array in this tutorial would be: $option01 and $option02.
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.
!is_array(get_option('my-plugin-options')) ? "" : extract(get_option('my-plugin-options'));
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.
$variable = array('option01' => $_POST['option01'], 'option02' => $_POST['option02']);
update_option('my-plugin-options', 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.
get_option('my-plugin-options') == "" ? "" : $new = unserialize(get_option('my-plugin-options'));
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 the number of options your plugin will require. It’s always a good idea to plan out your plugin ahead of time to get an idea of how many options are necessary for your project. If you need less than five options you can go with any method you desire; However, if your plugin requires more than ten options it’s 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.
frylock says:
thanx! awesome post. really helped me understand how to work with options in wordpress
Jürgen says:
Hello,
great article.
I was searching on information on how to implement arrays for saving options in my next wordpress plugin.
Jürgen
Carol says:
Much thanks for yet another awesome article. I am always on the look-out for fantastic WordPress tips to suggest to my own readers. Thanks for taking the time to write this article. It’s exactly what I was looking for. Truly fantastic post.
Bofy says:
Hi! I want to write a simple plugin for my WordPress blog but I didn’t find any documentation for what I need.
I want to have a settings/options page to save each user’s website URL (by user ID) on a separate admin menu page, not on the Profile page.
I want to give each Logged In user the possibility to save and see the settings page with their individual options saved (like on a profile page).
Can you post an example, please?
I would appreciate if you send me a link to an example or tutorial regarding my problem.
Thanks!
adam says:
post helped me alot! Thank you so much.
fransiska cute says:
thanks for sharing ryan. You made some good points there
your website is great. a lot information can i get from here 🙂
Home Network Nut says:
Is it applicable as is to WP 3.1? I tried to update my home-brewed plugin’s settings – it doesn’t work…
Ryan Christenson says:
These methods still work in WordPress
3.0.13.0.5.If you’re referring to WordPress 3.1 beta, I have no idea. I have yet to test on 3.1 beta, but I’m sure they work fine.
Edit: The methods do indeed still work in WordPress 3.1
Pályázatírás says:
I’m gonna try method one, I’m hoping to build a simple crm for WP, i hope this will help:)
skykery says:
Nice tutorial , Thanks ! I tried method three .
Yves Neault says:
Hi,
That was a great tutorial, thanks for sharing might try all of them lol.
Incontri says:
Does those methods also works in WP 3.1?
I’m trying to use them but still with no luck…
Ryan Christenson says:
@ Incontri;
Yes, these methods work in WordPress 3.1.
A.Razvan says:
I use this ‘methods’ sucsessfully in 3.1. wordpress. I dont use any other platform, wordpress is just great for all your needs. Isnt that right people?
Many salutes from Romania 🙂
Alex says:
These methods may work in the generic, function-based sense, but isn’t complete, nor practical, when it comes to admin/user interface.
Take, for example, the Codex ‘Create an Options Page’ that saves options via a form. http://codex.wordpress.org/Creating_Options_Pages
You can’t even find ‘update_options’ once on the page because it instead called options.php to handle the form and save all the options for you.
Now I’m not saying one way is better than the other, but it’s very misleading without context as to where and how you update these options.
Any clarification would be appreciated on both tutorials and the official Codex.
stheoret says:
Hi, i have the code below :
function on_activate()
{
$default = array(
‘gtcd’ => 1,
‘include’ => 0,
‘includeCache’ => 1,
‘iconPosition’ => ‘manual’,
‘iconLeftRight’ => ‘left’,
‘imageIcon’ => ”,
‘imageScale’ => 1.25,
‘headerAllPages’ => 0,
‘headerFont’ => ‘helvetica’,
‘headerFontSize’ => 10,
‘footerFont’ => ‘helvetica’,
‘footerFontSize’ => 10,
‘contentFont’ => ‘helvetica’,
‘contentFontSize’ => 8,
);
update_option(‘gorillasctopdf’, $default);
At plugin activation, all my defaults are correctly putted in the form but i want those to be “saved” in the database at activation too!!!
In this case, the user must save the options in the options menu for these values to be in the db.
Update_options do not save the values to the database….
All help appreciated!!!