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:
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_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.
a:4:{s:10:"list_begin";s:4:"<ul>";s:8:"list_end";s:5:"</ul>";s:15:"list_item_begin";s:4:"<li>";s:13:"list_item_end";s:5:"</li>";}
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().