Support » Plugins » How to save plugin options as an array in 2.5?

  • Hi,
    I’ve been storing my admin options in a single option as an array from some time now based on this tutorial.

    What process would I have to take to continue saving my options in an array based on the new option handling process mentioned here?

    In short, the above codex page’s example would save every field in my form as a separate option. How would I place each field into an array and save the array as a single option?


Viewing 7 replies - 1 through 7 (of 7 total)
  • You don’t need to use the “easy” option saving that is available. That is just an option for you if you want to do it that way. If you want to continue saving as an array, all you have to do is create an array containing all your option values and then call update_option('my_option', serialize($option_array));

    Thanks. I’ve never used serialize() before. I’ve just referenced my array. Guess I need to look into that.

    Just make sure you unserialize when it comes out of the option field, too, otherwise you’ll just have a big string of gobbledy good instead of an array.

    What’s the benefit of serialization?

    It allows an array to be stored as a string, particularly handy if you need to store data in a manner that does not require a set schema. Let’s say I had four options for a plugin:

        [list_begin] => <ul>
        [list_end] => </ul>
        [list_item_begin] => <li>
        [list_item_end] => </li>

    If I really did not want to create four separate options in the wp_options table, I could serialize that array (it works for objects too) and write that as one long string to be stored as one option.


    Once that is in the table, you can use the unserialize() function to return it to an array for use in your plugin. If you look at the /wp-admin/options.php page, you will see a good bit of the WordPress internals and some other plugins handle data in this way.

    Great explanation there, yearginsm, and pretty much the exact same I was going to give.

    Thanks for the explanation. I think WordPress must automatically do this because I never use the serialize and unserialize functions but my DB holds the info as described above and and I can foreach thorough it when i pull it back out via get_option().

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘How to save plugin options as an array in 2.5?’ is closed to new replies.