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:
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
update_option(), you will use
- // 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);
// 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
delete_site_option() but they should be self-explanatory now, shouldn’t they?