Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
To use one single function for stand-alone WordPress install and Multisite (in case options are network wide). Get/Update/Delete options with the same function with no need to check whether it's Multisite every time
//Check if plugin is Network activated
if (!function_exists('is_plugin_active_for_network')) require_once( ABSPATH . '/wp-admin/includes/plugin.php');
$plugins = get_plugins();
foreach( $plugins as $plugin_file => $plugin_info ) {
if ( $plugin_info['TextDomain'] === $this->plugin_name ){
$plugin = $plugin_file;
break;
}
}
//And set constant
if (is_plugin_active_for_network($plugin)) {
define("THIS_PLUGIN_NETWORK_ACTIVATED", true);
}
else {
define("THIS_PLUGIN_NETWORK_ACTIVATED", false);
}
// Instead of 'get_option' (regular WordPress) and 'get_site_option' (Multisite)
function get_this_plugin_option($option_name) {
if ('THIS_PLUGIN_NETWORK_ACTIVATED' == true) {
//Use Multisite function
return get_site_option($option_name);
}
else {
//Use regular WordPress function
return get_option($option_name);
}
}
// Instead of 'update_option' (regular WordPress) and 'update_site_option' (Multisite)
function update_this_plugin_option($option_name, $option_value) {
if ('THIS_PLUGIN_NETWORK_ACTIVATED' == true) {
return update_site_option($option_name, $option_value);
}
else {
return update_option($option_name, $option_value);
}
}
// Instead of 'delete_option' (regular WordPress) and 'delete_site_option' (Multisite)
function delete_this_plugin_option($option_name) {
if ('THIS_PLUGIN_NETWORK_ACTIVATED' == true) {
return delete_site_option($option_name);
}
else {
return delete_option($option_name);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.