I’m considering the saving of options for a plugin I’m developing. The first obvious candidate is the wp_options table.
While reading the Pro WP Plugin Development book I came across this:
Every option saved adds a new record in WordPress â option table. You can simply store several
options at once, in one array: This avoids cluttering the database and updates the values in one single MySQL query for greater efficiency and speed.
On the other hand, I also imagine that if the plugin has a lot of settings it may hinder performance to store everything in one array. Would it therefore make sense to split options into separate records in such cases?
Third option I can think of is to create custom tables to store the plugin settings, as I’ve seen some plugins do this as well.
What are the rules/guidelines to decide which way to go?
Store the options in a single array and write your plugin as a class. Load the options in the constructor and save it as a member variable and you’ll have access to it everywhere in the plugin.
Use the settings API. It’s awesome!
Here’s how you do it step by step.
This is a working code, the mark up can be whatever you want, make sure you retain the
settings_fields
anddo_settings_sections
call! The input field’s names should also be the way I mentioned.For detailed information, read this awesome series on wp tutsplus.