What is wrong with using add_option with Multisite instead of add_blog_option in a plugin

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

Read More

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’.

Related posts

Leave a Reply

2 comments

  1. add_blog_option ultimately calls add_option itself to add the option after switching the context to the supplied blog ID. the difference is that add_option will only work in the context of the current blog where add_blog_option lets you specify a blog ID that may be different than the current context.

  2. 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:

    1. Plugin is network activated – but it needs admin to complete setup (call it stateless)
    2. Plugin activated, no ‘unique features’ per blog and it doesn’t need an admin user to complete setup (call it active)
    3. Plugin activated, has unique features per blog and it does need an admin user to complete setup (call it pending)

    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.