WordPress Multisite: Global options that can be overridden per site

November 27, 2016

I recently ran into a situation where a WordPress Multisite install needed to have some options replicated across each site, both new and existing. It also needed to be able to override the options per site if so desired. The solution is fairly trivial.

  • Setup the options page in wp-admin. Ensure it exists on all subsites
  • On the main site fill in the values that should be defaulted across all sites
  • Drop the following code into your site and replace the placeholder with your own option name
  • Freely use the values from the main site or alter the options on a subsite

All you need to do to get this working is drop the following code into your site and change the placeholder to be the same as the option name that should be replicated.

add_filter( 'option_{OPTION_NAME}', 'my_option_settings', 10, 2 );
add_filter( 'default_option_{OPTION_NAME}', 'my_option_settings', 10, 2 );

function my_option_settings( $value, $option_name ) {
  // Do not loop on ourself
  if( 1 == get_current_blog_id() ) {
    // Bail out
    return $value;
  }
  if( ! $value ) {
    $value = get_blog_option( 1, $option_name );
  }
  return $value;
}
Leave a Reply

Your email address will not be published. Required fields are marked *