[wp-hackers] Best practices for plugin with complicated settings?

Andrew Nacin wp at andrewnacin.com
Sun Jul 25 07:06:59 UTC 2010


On Sun, Jul 25, 2010 at 2:49 AM, Anca Mosoiu <anca at anca.tv> wrote:
>
> Does the site settings table work the same way as post meta, where we can save
> multiple values with the same key.


Yep! And that's strongly encouraged in most situations. No serialization
needed of course; WP handles that for you. WP handles a lot more than that
actually, as you'll see with the settings API.

You can use register_setting() to register a single setting for your plugin.
Then make sure you have a sanitization callback that can verify that you're
storing each of your sub-settings individually.

For settings fields, you'd then use setting_name[option_a], etc... Then WP
will receive an array of your options, pass it to the single sanitization
callback, and eventually store it as a single row in the database.

In your plugin, you can then make a single call to get_option() and you'll
have everything you need as an array.

(That said, if you're storing a good amount of data and you don't need all
of it autoloaded, you *may* want to consider having two separate options,
one autoloaded and one not. Then only loading the fat option when you need
it.)

Anyway, here's two good tutorials, one by Ozh and one by Otto. Both of these
are linked from the Codex page:
http://codex.wordpress.org/Settings_API
http://ottodestruct.com/blog/2009/wordpress-settings-api-tutorial/
http://planetozh.com/blog/2009/05/handling-plugins-options-in-wordpress-28-with-register_setting/


More information about the wp-hackers mailing list