I develop a plugin that has a not too small number of settings. So far, there has been a fixed number of them, so I store them in a WordPress option. Now I consider letting users define (an arbitrary number of) styles, that is sets of options. Since these will still be rather general options, none of the categories listed in the Codex really fits.
Storing all that stuff in one option is clearly not a good idea, but creating a whole database tables does not feel right, either. What are your recommendations?
Note that I post one idea I have as an answer in order to separate it from the question. Would mark the question CW, but that seems to be disabled here?
The hierarchy of storing data roughly goes like this (from less functional/complex to more):
Since from your description Options are too limited and database too complex, I would look at Settings.
To best answer your question I suggest to not answer it 😉
Or more precisely: Encapsulate what varies.
As you right now do not know how / where to store your settings, create objects that are providing an interface to your store and that are hiding the whole storing away from the rest of your plugin.
Some example code:
Everywhere in your code you can then use
$store->get('settingName')
regardless of which storage layer you implement.You can now create another myStore_xxxx class implementing the myStore interface, e.g. for the styles. It will work exactly the same, but you can put the styles to somewhere else.
Normally you only need to read and write settings, so you would only need two functions here in the interface.
You can then – over time or while you continue to extend the number of options values / styles – change the storage methodology w/o affecting your plugin code.
Add one WordPress option per style. Since each style has a fixed size and is used in many places, the basic requirement for this technique in the Codex is fulfilled. Options should be named like
$pluginname_style_$name
.Retrieving one style with known name is very natural, idiomatic and (hopefully) fast then. Retrieving/deleting all styles is easily possible due to the naming scheme.
Assuming users do not create too many (i.e. at most tens, not hundreds) styles, the options table should not be bloated too much.
you can create an array as you dataset and save it into one option
for every new style
something like:
hope you get the idea.
The Settings API cares about making a performant array and store your options in only one DB-field. You can add to this field from different pages as long as you define to use the same field (“settings section” afaik) on other pages. Maybe you should use different sections for different parts (i.e. styles), so you could store it in a global avaible $var like
call:
or (even better):