Created
October 16, 2018 11:01
-
-
Save sabrina-zeidan/d1add9ecd43e18093dac4e60bf5ef6b4 to your computer and use it in GitHub Desktop.
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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