I’m working on a new plugin but it’s my first to save which will save an option to the database.
Currently I’m using add_option
and I assumed that
– activation would fail or
– the wrong value would be saved to the wp_blogID_options table
because I wasn’t using add_blog_option
. All the posts / literature (Wrox, Apress, etc) I’ve read say I need to use to add_blog_option
.
But all my testing (and inspecting the SQL table) are proved me wrong. So … What is wrong with using add_option instead of add_blog_option when making a plugin (that can be single install or multisite)?
To me … it seems that add_option
(or even get_option
) all work fine. I’m guessing the Settings API has some protection wrapper.
However … that would mean using add_blog_option
(or get_blog_option) are only for ‘purists’.
add_blog_option
ultimately callsadd_option
itself to add the option after switching the context to the supplied blog ID. the difference is thatadd_option
will only work in the context of the current blog whereadd_blog_option
lets you specify a blog ID that may be different than the current context.What I’ve come to ‘learn’ is that there are 3 states on plugin activation for a mulitsite setup.
Consider this for a Site Admin who installs a new plugin:
Scenario 1: you can safely use ‘add_option’ on plugin activation (or network activation) as the plugin is stateless.
Scenario 2: you must use ‘add_blog_option’ and loop through each blog – if you do network activation as the plugin will be active.
Scenario 3: you must use ‘add_blog_option’ and ideally you don’t allow network activation because the plugin is pending.
In my case, my plugin does nothing until the Blog owner creates a page with the shortcode.