get_option compatible with wordpress network (multisite)?

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

1 Answer
1

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.

Leave a Comment