1

I have this function in a plugin that works on wp install but it doesnt work on wordpress MU.

 if( !get_option( $option-name ) ) {
        // something
            } else {
    // something else
    }

How can I make the get_option compatible with Multisite install.

// I am using redux framework, I created an option and I wanted to have a conditional to check if the option is used or not.. but it worked fine on the wp install but not on MU one.

Thanks

  • How can I ask about that then? – dzdesigner Oct 26 '14 at 10:31
  • The same way you write code, in a precise and complete way. Don't let the reader guess what you mean. – Mark Kaplun Oct 26 '14 at 12:02
  • get_option() is completely compatible with multisite, given that it's one of the most used APIs the abhorrent amount of things wouldn't work otherwise. Please elaborate and what is your expected and actual result. – Rarst Oct 26 '14 at 13:53
  • I think you are looking for get_site_option(). codex.wordpress.org/Function_Reference/get_site_option. get_site_option() pulls from the site_meta table... while get_option pulls from the corresponding sites wp_options table. – josh Oct 26 '14 at 14:55
4

When working with multisites, there are two commonly used sets of functions for pulling/updated options.

Now... how you structure your options and save them to the database is up to you. It depends on the functionality your plugin is adding, and where you want to store/pull options.

Single Site Configuration

get_option() and update_option() are used to interact with the wp_options table. This table is specific to each blog id. You may have a wp_options_2 and a wp_options_3 table; where 2 and 3 correspond to that blogs id.

If you use get_option() in your code... it is going to look for that option in the corresponding blog id's wp_options table. This means every sub-site could have a different value here.

Multi Site Configuration

The other set of functions are get_site_option() and update_site_option(). These two functions are used to interact with the site_meta table. The site_meta table exists only once.. and is used for global options for all subsites.

If you use get_site_option() in your code... it is going to pull that option from the site_meta table. In other words, each subsite will use the same option from the site_meta table.

Custom Code to Check if Multisite is Active

What I do when I code a plugin that needs to be multisite compatible.. is add a custom function to check and see if the site is a network install. If it is... I pull from site meta... if it is not, I pull from site options.

// First, I define a constant to see if site is network activated
if ( ! function_exists( 'is_plugin_active_for_network' ) ) {
    // Makes sure the plugin is defined before trying to use it
    require_once( ABSPATH . '/wp-admin/includes/plugin.php' );
}
if (is_plugin_active_for_network('my_plugin_folder/main.php')) {  // path to plugin folder and main file
    define("THIS_PLUGIN_NETWORK_ACTIVATED", true);
}
else {
    define("THIS_PLUGIN_NETWORK_ACTIVATED", false);
}

// Wordpress function 'get_site_option' and 'get_option'
function get_this_plugin_option($option_name) {

    if(THIS_PLUGIN_NETWORK_ACTIVATED== true) {

        // Get network site option
        return get_site_option($option_name);
    }
    else {

        // Get blog option
        return get_option($option_name);
    }
}

// Wordpress function 'update_site_option' and 'update_option'
function update_this_plugin_option($option_name, $option_value) {

    if(THIS_PLUGIN_NETWORK_ACTIVATED== true) {

        // Update network site option
        return update_site_option($option_name, $option_value);
    }
    else {

    // Update blog option
    return update_option($option_name, $option_value);
    }
}

Now, we can simply use get_this_plugin_option() or update_this_plugin_option(); and our code will determine if the site is network activated or not.. and update or get the option from the corresponding options table.

This prevents having to write code twice.. or use two separate hooks when coding for network activations.

  • Thanks alot for everyone and thanks to Josh for the detailed tutorial, It helped me alot to understand how things go with the get_option() and get_site_option()..I am actually using redux framework. I created an option and I wanted to make the conditional that checks IF the option is used or not.. and I've got stuck in the MU framework.. I will try out all what you said. and for I think I should edit my question to explain my question better. Thanks to everyone – dzdesigner Oct 26 '14 at 17:47
  • 1
    I just modified lazy-invitation plugin with this. Thank you so much... – randomblink Sep 18 '15 at 1:37
  • You're extremely welcome. I still use the code to this day :) Sorry for late reply; lost my login info for the longest time! Stay awesome!! – josh Feb 14 '16 at 21:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.