Skip to content

Instantly share code, notes, and snippets.

@spacedmonkey
Last active September 15, 2015 23:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save spacedmonkey/57135e6546dcf54fa527 to your computer and use it in GitHub Desktop.
Save spacedmonkey/57135e6546dcf54fa527 to your computer and use it in GitHub Desktop.
/**
* Retrieve site option value based on name of option.
*
* @since 2.8.0
*
* @see get_option()
*
* @global wpdb $wpdb
*
* @param string $option Name of option to retrieve. Expected to not be SQL-escaped.
* @param mixed $default Optional value to return if option doesn't exist. Default false.
* @return mixed Value set for the option.
*/
function get_network_option( $option, $default = false, $network_id = false ) {
global $wpdb;
if( ! $network_id ){
$current_network = get_current_site();
$network_id = $current_network->id;
}
/**
* Filter an existing site option before it is retrieved.
*
* The dynamic portion of the hook name, `$option`, refers to the option name.
*
* Passing a truthy value to the filter will effectively short-circuit retrieval,
* returning the passed value instead.
*
* @since 2.9.0 As 'pre_site_option_' . $key
* @since 3.0.0
* @since 4.4.0 The `$option` parameter was added
*
* @param mixed $pre_option The default value to return if the option does not exist.
* @param string $option Option name.
*/
$pre = apply_filters( 'pre_site_option_' . $option, false, $option );
if ( false !== $pre )
return $pre;
// prevent non-existent options from triggering multiple queries
$notoptions_key = "{$network_id}:notoptions";
$notoptions = wp_cache_get( $notoptions_key, 'site-options' );
if ( isset( $notoptions[$option] ) ) {
/**
* Filter a specific default site option.
*
* The dynamic portion of the hook name, `$option`, refers to the option name.
*
* @since 3.4.0
* @since 4.4.0 The `$option` parameter was added.
*
* @param mixed $default The value to return if the site option does not exist
* in the database.
* @param string $option Option name.
*/
return apply_filters( 'default_site_option_' . $option, $default, $option );
}
if ( ! is_multisite() ) {
/** This filter is documented in wp-includes/option.php */
$default = apply_filters( 'default_site_option_' . $option, $default, $option );
$value = get_option($option, $default);
} else {
$value = get_network_meta($network_id, $option, true);
// Has to be get_row instead of get_var because of funkiness with 0, false, null values
if ( false !== $value ) {
$value = maybe_unserialize( $value );
} else {
if ( ! is_array( $notoptions ) ) {
$notoptions = array();
}
$notoptions[$option] = true;
wp_cache_set( $notoptions_key, $notoptions, 'site-options' );
/** This filter is documented in wp-includes/option.php */
$value = apply_filters( 'default_site_option_' . $option, $default, $option );
}
}
/**
* Filter the value of an existing site option.
*
* The dynamic portion of the hook name, `$option`, refers to the option name.
*
* @since 2.9.0 As 'site_option_' . $key
* @since 3.0.0
* @since 4.4.0 The `$option` parameter was added
*
* @param mixed $value Value of site option.
* @param string $option Option name.
*/
return apply_filters( 'site_option_' . $option, $value, $option );
}
function add_network_meta( $network_id, $meta_key, $meta_value, $unique = false ) {
if( ! is_multisite() ){
return false;
}
return add_metadata('site', $post_id, $meta_key, $meta_value, $unique);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment