Archive for November, 2010

Override WordPress Plugin Options for All Sites in MultiSite

These days, enabling a plugin for all sites in WordPress MultiSite is a breeze… you just install the plugin files, then click “Network Activate” from any site’s plugins page.  However, configuring plugins on a site-wide basis is a different matter.  If you don’t want to use the default settings for a plugin, or if you want to be sure the settings to be the same for all sites in your network, you’re forced to go through one site at a time and change the settings manually.

I’d run into this problem before, and since I’m typically up against a mountain of work, I’ve never had time to solve it in an elegant way.  But with flexibility of WordPress, I was sure there was a hook or action somewhere that could help make this a bit easier.

Using the “pre_option_{option_name}” Filter

Turns out, it’s pretty easy to override options saved in the database.  Just use the pre_option_{option_name} filter or the option_{option_name} filter.  As I understand it, you should use the first if you simply want to replace the option’s value outright, and the second if you want to manipulate the value that’s saved in the database.  For example, if you wanted to change the value of the “blogname” option, you could add a filter to “pre_option_blogname”.

Gotcha: Limits of Filtering Options from Within A Theme

Typically, I put this kind of hack somewhere in my functions.php file in my theme (I’m using the same theme for all sites in the network).  However, after a bit of debugging, I realized that plugins are loaded before the theme files (makes sense), so if you set up a pre_option_ filter in your theme, you’ll filter the options in time for use in your theme itself, but not in time to see the values reflected in the admin pages.

To solve that, I figured I’d just try to add the filter earlier…

Solved: Override Options with “Must Use” Plugins

Basically, I just created a “must use” plugin — which is just a php file in the wp-content/mu-plugins folder that WordPress loads automatically.  I named the file to match the plugin whose settings I was overriding… i.e. if the original plugin was called “foobar”, I called my plugin “foobar-settings.php”.  From within that file, I added the “pre_option_” filters I wanted, and viola, everything worked as expected.

Of course, things being every more complex, I also had to override the creation of some custom terms by the original plugin — also accomplished from my mu-plugin using register_activation_hook.  And then I had to install another mu-plugin called Proper Network Activation to be sure the plugin activation hooks were fired for all blogs when the plugin is network-activated.  But that’s a story for another time.