WordPress: Working With Options

Options in WordPress are in an extra options table (with Prefix before) stored. Options include the blog name, which theme is used, etc.. Plugins define their settings as options. A simple example of how options can be accessed:

echo get_option('blogname');

This snippet is the blog name. In order to use its own options, they must be created first:

add_option('myOptions', 'a Test');

This call writes a new entry myOptions with the value “a Test” in the database. If you have more data to store, you do not have to write every option in a new row of the database table. Taking the data in an array provides a lot faster access.

$myOptions = array(
	'firstOption' => 1,
	'secondOption' => 'second'
);
add_option('myOptions', $myOptions);

In the database it looks like this:

a:2:{s:11:"firstOption";i:1;s:12:"secondOption";s:6:"second";}

Here you can see the data type very nicely. a: is for array, s: for a string and i: for an integer.
To overwrite the options, you use update_option().

$myNewOptions = array(
	'firstOption' => 2,
	'secondOption' => 'my second Option'
);
update_option('myOptions', $myNewOptions);

Now you can work with the stored options.

$myOptions = get_option('MyOptions');
foreach($myOptions as $option => $value) {
	echo $option . " => " . $value . "
"; } /* Output: firstOption => 2 secondOption => my second Option */ echo $myOptions['firstOption']; /* Output: 2 */

Options of a plugin or theme should be also deleted if you uninstall them. You delete the options with delete_option.

delete_option('myOptions');

You also can display something depending on the options, if option is 0, do this, if it’s 1 do this.

In my theme, which I’m working on right now, I have over 130 options now. It’s not very easy to program, if you must access nested arrays.

$myOptions = get_option('MyOptions');
echo $myOptions['first']['foo']['bar']['name];

A really cool alternative is to write the options as an object in the database:

$myOptions = new stdClass();
$myOptions->name = "myOption";
$myOptions->params->param1 = 1;
$myOptions->params->param2 = 'second parameter';
update_option('myOptions', $myOptions);

In our database is now our serialized Object:

O:8:"stdClass":2:{s:4:"name";s:8:"myOption";s:6:"params";O:8:"stdClass":2:{s:6:"param1";i:1;s:6:"param2";s:16:"second parameter";}}

After you grab the options from your database, they are available as object.

$myOptions = get_option('MyOptions');
echo $myOptions->name;
/* Output:
myOption
*/

echo $myOptions->params->param2;
/* Output:
second parameter
*/

What do you do now with the acquired knowledge? I do not know, use your creativity and leave your ideas in the comment section!


Posted

in

by

Comments

17 responses to “WordPress: Working With Options”

  1. Alex Avatar

    Great article guys! This is something I’ve been needing to know about for a while.

  2. Peter Avatar

    I always assumed that both arrays and objects needed to be explicitly serialized (with serialize()) before they were stored in a database. Is this not the case? Does wordpress do this for you with add_option, or is it just unneccesary all together?

    Obviously I need to do some reading on the subject…

  3. Michael Avatar

    Thanks Alex 🙂

    Peter, WordPress does the job for you. add_option and update_option calls internal maybe_serialize().
    See function maybe_serialize() in wp-includes/functions.php at line 801.

  4. Peter Avatar

    Ahh.. Brilliant. WordPress strikes again!

  5. kubi Avatar

    Thanks! I always thought that i have to do serialize and unserialize, but good to know that wordpress does handle that for me.

  6. Ozh Avatar

    Nice article for beginners.
    But I was expecting something about how you need to register options for 2.8 🙂

  7. Michael Avatar

    Ozh, you mean the set_/get_transient stuff?
    I think, many plugin authors should learn first to use one tablerow instead of 60. Than we can write about set_transient 😛

  8. […] Diejenigen, die immer noch jede einzelne Option separat speichern, müssen die Zeile delete_option('your_theme_options') selbstverständlich entsprechend erweitern. Daher gilt, besser die Einstellungen in einem Array oder Objekt zu speichern und performanter zu arbeiten. Weitere Hintergründe gibt es im Beitrag WordPress: Working With Options […]

  9. Dario Ferrer Avatar

    Great post. Handling data as objects is an excellent method. However I think the “add_option” hook can be replaced with “update_option”, which also can creates new fields.

    By the way I have a question ¿how to delete only one of those elements, e.g. “param2”?. At this moment I’m some stucked with this issue.

  10. Michael Avatar

    Dario, depends on what you need. Override the array or object with a new one and update_otions. Or set the value of the param2 to ” or 0.

  11. Dario Ferrer Avatar

    I wish to set the param2 to 0, or better, delete it. But “0” is fine also. Thanks.

  12. Dario Ferrer Avatar

    Well, I solved the issue with some conditionals on data (before send it to DB), but I’m still curious about some direct hook which allows delete (or at least set to 0) an array element placed into some option.

  13. Ajay Avatar

    How do you implement this with WordPress themes? The tutorial I followed adds a separate row for each option!

  14. krillo Avatar
    krillo

    Hey, good info.
    It gave me the clue how to read wp_options values from one wpmu blog to another. This is how i did it:

        global $wpdb;
        $args = $wpdb->get_results("select * from wp_6_options where option_name = 'widget_zmags_thumb'");
        $options = $args[0]->option_value;
        $zmags = maybe_unserialize($options);
    

    Where I used maybe_unserialize() from wp-includes/functions.php to unserialize the data to an array.
    Cheers!

  15. Darío Ferrer Avatar

    krillo,

    $args = $wpdb->get_results( bla bla bla…
    $args = maybe_unserialize( $args );

  16. Darío Ferrer Avatar

    Sorry krillo, the above code is wrong, this is the right

    $args = $wpdb->get_results( bla bla bla…
    $options = maybe_unserialize( $args->option_value );

    Then you can use it in this way:

    echo $options[‘your_array_element’];

  17. Bofy Avatar
    Bofy

    I want to write a users options page as a plugin and I can’t find any example of code.
    I need to save each user’s website url on a page (by user ID) in admin panel, but not on Profile page.
    Can you post, please, an example?
    Thanks!