Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
/**
* 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
You can’t perform that action at this time.