How to save plugin options as an array in 2.5?
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?
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
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.
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:
Array ( [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_optionstable, 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.phppage, 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.
- The topic ‘How to save plugin options as an array in 2.5?’ is closed to new replies.