Back to Top

Global Options in a Multi-site WordPress

Previous Post:

Global Options in a Multi-site WordPress

If you are developing a theme or plugin and plan to provide support for a network installation of WordPress, you should have heard about two useful WPMU functions: get_site_option() and update_site_option(), which have been merged into WordPress as of version 3.0. Now why are they so important in developing theme or plugin? The answer is simple: there are plugin or theme settings that should only be set by a super admin (or network admin) and those settings should be applied to all blogs within the network.

Imagine that you are writing a plugin that needs a cache folder and you would like your users to be able to specify a custom cache folder if they desire. Things should be easy if your plugin is installed in a normal WordPress, but if it’s a Multi-site WordPress, all admins will have the ability to choose a cache folder, which is of course not ideal. The cache folder should be set by the network admin only and should be saved into a global option so that it will be applied to all blogs within your network.

Such thing can be done easily with the help of the two functions we are discussing about. When it is a global option (or network-wide option), instead of using get_option() and update_option(), you will use get_site_option() and update_site_option():

// blog-specific options
$blog_options = get_option('myplugin_blog_option');
update_option('myplugin_blog_option', $blog_options);
// network-wide options
$network_site_options = get_site_option('myplugin_network_option');
update_site_option('myplugin_network_option', $blog_options);

Simple as that! All global options will then be saved in, by default, wp_sitemeta table (a network-wide table) instead of the regular wp_options table (a blog-specific table).

Note that in this tip I did not mention add_site_option() and delete_site_option() but they should be self-explanatory now, shouldn’t they? πŸ˜‰

Take Social Sharing to
the Next Level with Monarch!

Take Social Sharing to the Next Level with Monarch!
Print Article Trackback Trackback to this Article   Subscribe to Comments RSS Subscribe to Comments RSS

19 Opinions for Global Options in a Multi-site WordPress

  1. User's Gravatar
    Scudo April 12, 2011 at 10:59 pm – Permalink

    Just found out about this article through Google, very nice tip indeed! I’m developing a multisite plugin so this will come in handy, thanks!

  2. User's Gravatar
    options trading April 13, 2011 at 10:22 am – Permalink

    Glad to see this information.its very interesting.I would like to hear more information from your side.

  3. User's Gravatar
    Mike July 28, 2011 at 12:43 am – Permalink

    Shouldn’t Line 5 call get_site_option()

    • User's Gravatar
      OddOneOut July 28, 2011 at 11:08 am – Permalink

      Thanks, fixed!

  4. User's Gravatar
    Eric Davis August 5, 2011 at 5:06 am – Permalink

    Do you know if the update_ functions will also create a key if it doesn’t exist? I’d like to be able to just use one function instead of having to check if it exists (get) and then call add/update.

    • User's Gravatar
      OddOneOut August 5, 2011 at 8:48 am – Permalink

      Yes they do. The update functions work like this:
      – If does not exist, add the option.
      – If exists, update the option.

  5. User's Gravatar
    vannie November 16, 2011 at 2:10 pm – Permalink

    It’s a great post!.. It helps a lot to me since I’m still learning how to develop multi-site wordpress. Thanks for sharing it!

  6. User's Gravatar
    Chas November 21, 2011 at 2:50 am – Permalink

    Thanks, this is a great idea. I have built a few themes and never considered that there should be a global option for this. I have built a number of sites on a server with common areas, but not really thought about this for other clients.

    WIll give this a proper read and see if I can add this to my templates.


  7. User's Gravatar
    Christian Carter January 20, 2012 at 4:45 pm – Permalink

    I am not a theme developer but I do use so many different wordpress themes for my websites and I like to modify them most of the time. Sometimes different themes work differently so I really need this kind of tutorial to know what to do/modify.

  8. User's Gravatar
    John Goold April 22, 2012 at 2:36 am – Permalink

    The problem I wanted to solve was to ensure that ALL “sites” ALWAYS saw the same options. The particular plugin I tried this out on is “TinyMCE Advanced”. I deactivated it, replaced a couple of modified (hacked?) PHP files in the plugin then activated it. I did this because I wanted to delete any per-site setting for this plugin (and I was hoping this would do the trick). I then changed a couple of the toolbar settings using the “Primary site” admin login.
    This appeared to work fine. But there is a got’cha. If a non-primary “site” owner logs in, he (or she) can change the TinyMCE Advanced settings. I was testing this to see if individual “sites” would end up being able to override settings. Unfortunately, what I suspected would happen, did happen. It changed the GLOBAL settings.
    Do you happen to know of any simple way to prevent this, for example, a way to make the “TinyMCE Advanced” (or any other plugin’s) “Settings” invisible to non-primary “sites”?

    • User's Gravatar
      OddOneOut April 22, 2012 at 10:17 pm – Permalink

      That depends a lot on how a plugin builds its option pages. Generally, you can use something like this to hide network-wide options from simple admins:

      if (function_exists('is_multisite') && is_multisite() && is_super_admin())
      	// Show super-admin only options here
      • User's Gravatar
        John Goold April 22, 2012 at 11:19 pm – Permalink

        Thank you for the very fast response!
        I suspected it would have to be a plugin-by-plugin solution β€” so back to code hacking πŸ™‚
        By the way, I like the way you show code snippets on your site and am about to read your post about your plugin.

  9. User's Gravatar
    John Goold April 23, 2012 at 12:07 am – Permalink

    I changed the particular plugin to use global (i.e. site) options rather than user options, which is why I wanted to disable “ordinary admins” or users from changing them. I know the correct solution would be to use the global setting only if a user has NOT set his or her own, but I don’t particularly want to create an entirely new version of a plugin that the real developers keep improving β€” with minimum changes I can simply re-apply them.
    Anyway, the settings/administration code starts out:

    if ( !defined('TADV_ADMIN_PAGE') || !current_user_can('manage_options') )
    	wp_die('Access denied');

    which I’ve changed to:

    /* 2012-04-22 JRG Only allow the Super Admin to change options */
    if ( !defined('TADV_ADMIN_PAGE') || !current_user_can('manage_network_options') )
    	wp_die('Access denied');

    It’s a hack, but it works.

    • User's Gravatar
      OddOneOut April 23, 2012 at 7:55 pm – Permalink

      I have merged the two comments and fixed the codeblocks for you, hope you don’t mind :).

  10. User's Gravatar
    Green July 13, 2012 at 9:51 pm – Permalink

    I’m looking HARD and I am getting nowhere fast for a solution!

    So here goes:
    I’m using ddean’s Networks-For-Wordpress plugin to create multiple networks.
    In a multi-network setup (As in multiple networks of sites) the get_site_option only gets settings for sites for the network that the site in question is in. How would one retrieve a setting from the main master site?

    I was thinking that I would have to use switch_to_blog(1) but that doesn’t seem to work….

    Have you ever ran across this?
    I know you’re not here to just answer peoples dumb questions, but I thought this maybe interesting enough for you to look into. Cool Site btw.

    Thanks for listening!!! =D

  11. User's Gravatar
    Green September 28, 2012 at 1:45 pm – Permalink

    Guess not! I solved my own problem using a class and pre-site-options with custom wpdb queries.

    Not much conversation in here is there?

  12. User's Gravatar
    Ivan Ferrer May 29, 2013 at 11:50 pm – Permalink

    Thanks! This is the kind of issue you discover when your site goes to production, not in development! πŸ™‚

  13. User's Gravatar
    Klyles March 4, 2016 at 3:28 am – Permalink

    I have a question, if there is a way to do this for all plugin options?

    So in so many words 1 main plugin screen with all then plugins, and the plugin settings will be sitewide, unless other options are entered in the site plugin settings by super admin.

    • User's Gravatar
      Khang Minh March 4, 2016 at 9:28 am – Permalink

      You should add a network setting page and consider any changes submitted on that page as sitewide. Settings saved when inside a specific blog should be specific to that blog only.

Speak Up Your Mind!

An asterisk (*) indicates a required field and must be filled.

  • Web page and e-mail addresses turn into links automatically.
  • Wrap codes in: <code lang=""></code> or <pre lang="" extra="">
  • Lines and paragraphs break automatically.

Next Post: