function get_network_option( $network_id, $option, $default_value = false ) {
if ( $network_id && ! is_numeric( $network_id ) ) {
$network_id = (int) $network_id;
// Fallback to the current network if a network ID is not specified.
$network_id = get_current_network_id();
* Filters the value of an existing network option before it is retrieved.
* The dynamic portion of the hook name, `$option`, refers to the option name.
* Returning a value other than false from the filter will short-circuit retrieval
* and return that value instead.
* @since 2.9.0 As 'pre_site_option_' . $key
* @since 4.4.0 The `$option` parameter was added.
* @since 4.7.0 The `$network_id` parameter was added.
* @since 4.9.0 The `$default_value` parameter was added.
* @param mixed $pre_site_option The value to return instead of the option value. This differs from
* `$default_value`, which is used as the fallback value in the event
* the option doesn't exist elsewhere in get_network_option().
* Default false (to skip past the short-circuit).
* @param string $option Option name.
* @param int $network_id ID of the network.
* @param mixed $default_value The fallback value to return if the option does not exist.
$pre = apply_filters( "pre_site_option_{$option}", false, $option, $network_id, $default_value );
// Prevent non-existent options from triggering multiple queries.
$notoptions_key = "$network_id:notoptions";
$notoptions = wp_cache_get( $notoptions_key, 'site-options' );
if ( is_array( $notoptions ) && isset( $notoptions[ $option ] ) ) {
* Filters the value of a specific default network option.
* The dynamic portion of the hook name, `$option`, refers to the option name.
* @since 4.4.0 The `$option` parameter was added.
* @since 4.7.0 The `$network_id` parameter was added.
* @param mixed $default_value The value to return if the site option does not exist
* @param string $option Option name.
* @param int $network_id ID of the network.
return apply_filters( "default_site_option_{$option}", $default_value, $option, $network_id );
if ( ! is_multisite() ) {
/** This filter is documented in wp-includes/option.php */
$default_value = apply_filters( 'default_site_option_' . $option, $default_value, $option, $network_id );
$value = get_option( $option, $default_value );
$cache_key = "$network_id:$option";
$value = wp_cache_get( $cache_key, 'site-options' );
if ( ! isset( $value ) || false === $value ) {
$row = $wpdb->get_row( $wpdb->prepare( "SELECT meta_value FROM $wpdb->sitemeta WHERE meta_key = %s AND site_id = %d", $option, $network_id ) );
// Has to be get_row() instead of get_var() because of funkiness with 0, false, null values.
if ( is_object( $row ) ) {
$value = $row->meta_value;
$value = maybe_unserialize( $value );
wp_cache_set( $cache_key, $value, 'site-options' );
if ( ! is_array( $notoptions ) ) {
$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_value, $option, $network_id );
if ( ! is_array( $notoptions ) ) {
wp_cache_set( $notoptions_key, $notoptions, 'site-options' );
* Filters the value of an existing network option.
* The dynamic portion of the hook name, `$option`, refers to the option name.
* @since 2.9.0 As 'site_option_' . $key
* @since 4.4.0 The `$option` parameter was added.
* @since 4.7.0 The `$network_id` parameter was added.
* @param mixed $value Value of network option.
* @param string $option Option name.
* @param int $network_id ID of the network.
return apply_filters( "site_option_{$option}", $value, $option, $network_id );
* Adds a new network option.
* Existing options will not be updated.
* @global wpdb $wpdb WordPress database abstraction object.
* @param int|null $network_id ID of the network. Can be null to default to the current network ID.
* @param string $option Name of the option to add. Expected to not be SQL-escaped.
* @param mixed $value Option value, can be anything. Expected to not be SQL-escaped.
* @return bool True if the option was added, false otherwise.
function add_network_option( $network_id, $option, $value ) {
if ( $network_id && ! is_numeric( $network_id ) ) {
$network_id = (int) $network_id;
// Fallback to the current network if a network ID is not specified.
$network_id = get_current_network_id();
wp_protect_special_option( $option );
* Filters the value of a specific network option before it is added.
* The dynamic portion of the hook name, `$option`, refers to the option name.
* @since 2.9.0 As 'pre_add_site_option_' . $key
* @since 4.4.0 The `$option` parameter was added.
* @since 4.7.0 The `$network_id` parameter was added.
* @param mixed $value Value of network option.
* @param string $option Option name.
* @param int $network_id ID of the network.
$value = apply_filters( "pre_add_site_option_{$option}", $value, $option, $network_id );
$notoptions_key = "$network_id:notoptions";
if ( ! is_multisite() ) {
$result = add_option( $option, $value, '', false );
$cache_key = "$network_id:$option";
* Make sure the option doesn't already exist.
* We can check the 'notoptions' cache before we ask for a DB query.
$notoptions = wp_cache_get( $notoptions_key, 'site-options' );
if ( ! is_array( $notoptions ) || ! isset( $notoptions[ $option ] ) ) {
if ( false !== get_network_option( $network_id, $option, false ) ) {
$value = sanitize_option( $option, $value );
$serialized_value = maybe_serialize( $value );
'site_id' => $network_id,
'meta_value' => $serialized_value,
wp_cache_set( $cache_key, $value, 'site-options' );
// This option exists now.
$notoptions = wp_cache_get( $notoptions_key, 'site-options' ); // Yes, again... we need it to be fresh.
if ( is_array( $notoptions ) && isset( $notoptions[ $option ] ) ) {
unset( $notoptions[ $option ] );
wp_cache_set( $notoptions_key, $notoptions, 'site-options' );
* Fires after a specific network option has been successfully added.
* The dynamic portion of the hook name, `$option`, refers to the option name.
* @since 2.9.0 As "add_site_option_{$key}"
* @since 4.7.0 The `$network_id` parameter was added.
* @param string $option Name of the network option.
* @param mixed $value Value of the network option.
* @param int $network_id ID of the network.
do_action( "add_site_option_{$option}", $option, $value, $network_id );
* Fires after a network option has been successfully added.
* @since 4.7.0 The `$network_id` parameter was added.
* @param string $option Name of the network option.
* @param mixed $value Value of the network option.
* @param int $network_id ID of the network.
do_action( 'add_site_option', $option, $value, $network_id );
* Removes a network option by name.
* @global wpdb $wpdb WordPress database abstraction object.
* @param int|null $network_id ID of the network. Can be null to default to the current network ID.
* @param string $option Name of the option to delete. Expected to not be SQL-escaped.
* @return bool True if the option was deleted, false otherwise.
function delete_network_option( $network_id, $option ) {
if ( $network_id && ! is_numeric( $network_id ) ) {
$network_id = (int) $network_id;
// Fallback to the current network if a network ID is not specified.
$network_id = get_current_network_id();
* Fires immediately before a specific network option is deleted.
* The dynamic portion of the hook name, `$option`, refers to the option name.
* @since 4.4.0 The `$option` parameter was added.
* @since 4.7.0 The `$network_id` parameter was added.
* @param string $option Option name.
* @param int $network_id ID of the network.
do_action( "pre_delete_site_option_{$option}", $option, $network_id );
if ( ! is_multisite() ) {
$result = delete_option( $option );
$row = $wpdb->get_row( $wpdb->prepare( "SELECT meta_id FROM {$wpdb->sitemeta} WHERE meta_key = %s AND site_id = %d", $option, $network_id ) );
if ( is_null( $row ) || ! $row->meta_id ) {
$cache_key = "$network_id:$option";
wp_cache_delete( $cache_key, 'site-options' );
'site_id' => $network_id,
$notoptions_key = "$network_id:notoptions";
$notoptions = wp_cache_get( $notoptions_key, 'site-options' );
if ( ! is_array( $notoptions ) ) {
$notoptions[ $option ] = true;
wp_cache_set( $notoptions_key, $notoptions, 'site-options' );
* Fires after a specific network option has been deleted.
* The dynamic portion of the hook name, `$option`, refers to the option name.
* @since 2.9.0 As "delete_site_option_{$key}"
* @since 4.7.0 The `$network_id` parameter was added.
* @param string $option Name of the network option.
* @param int $network_id ID of the network.
do_action( "delete_site_option_{$option}", $option, $network_id );
* Fires after a network option has been deleted.
* @since 4.7.0 The `$network_id` parameter was added.
* @param string $option Name of the network option.
* @param int $network_id ID of the network.
do_action( 'delete_site_option', $option, $network_id );
* Updates the value of a network option that was already added.
* @global wpdb $wpdb WordPress database abstraction object.
* @param int|null $network_id ID of the network. Can be null to default to the current network ID.
* @param string $option Name of the option. Expected to not be SQL-escaped.
* @param mixed $value Option value. Expected to not be SQL-escaped.
* @return bool True if the value was updated, false otherwise.
function update_network_option( $network_id, $option, $value ) {
if ( $network_id && ! is_numeric( $network_id ) ) {
$network_id = (int) $network_id;
// Fallback to the current network if a network ID is not specified.
$network_id = get_current_network_id();
wp_protect_special_option( $option );
$old_value = get_network_option( $network_id, $option );
* Filters a specific network option before its value is updated.
* The dynamic portion of the hook name, `$option`, refers to the option name.
* @since 2.9.0 As 'pre_update_site_option_' . $key
* @since 4.4.0 The `$option` parameter was added.
* @since 4.7.0 The `$network_id` parameter was added.
* @param mixed $value New value of the network option.
* @param mixed $old_value Old value of the network option.
* @param string $option Option name.
* @param int $network_id ID of the network.
$value = apply_filters( "pre_update_site_option_{$option}", $value, $old_value, $option, $network_id );
* If the new and old values are the same, no need to update.
* Unserialized values will be adequate in most cases. If the unserialized
* data differs, the (maybe) serialized data is checked to avoid
* unnecessary database calls for otherwise identical object instances.
* See https://core.trac.wordpress.org/ticket/44956
if ( $value === $old_value || maybe_serialize( $value ) === maybe_serialize( $old_value ) ) {
if ( false === $old_value ) {
return add_network_option( $network_id, $option, $value );
$notoptions_key = "$network_id:notoptions";
$notoptions = wp_cache_get( $notoptions_key, 'site-options' );
if ( is_array( $notoptions ) && isset( $notoptions[ $option ] ) ) {
unset( $notoptions[ $option ] );
wp_cache_set( $notoptions_key, $notoptions, 'site-options' );
if ( ! is_multisite() ) {
$result = update_option( $option, $value, false );
$value = sanitize_option( $option, $value );
$serialized_value = maybe_serialize( $value );
array( 'meta_value' => $serialized_value ),
'site_id' => $network_id,
$cache_key = "$network_id:$option";
wp_cache_set( $cache_key, $value, 'site-options' );
* Fires after the value of a specific network option has been successfully updated.
* The dynamic portion of the hook name, `$option`, refers to the option name.
* @since 2.9.0 As "update_site_option_{$key}"
* @since 4.7.0 The `$network_id` parameter was added.
* @param string $option Name of the network option.
* @param mixed $value Current value of the network option.
* @param mixed $old_value Old value of the network option.
* @param int $network_id ID of the network.
do_action( "update_site_option_{$option}", $option, $value, $old_value, $network_id );
* Fires after the value of a network option has been successfully updated.
* @since 4.7.0 The `$network_id` parameter was added.
* @param string $option Name of the network option.
* @param mixed $value Current value of the network option.
* @param mixed $old_value Old value of the network option.
* @param int $network_id ID of the network.
do_action( 'update_site_option', $option, $value, $old_value, $network_id );
* Deletes a site transient.
* @param string $transient Transient name. Expected to not be SQL-escaped.
* @return bool True if the transient was deleted, false otherwise.
function delete_site_transient( $transient ) {
* Fires immediately before a specific site transient is deleted.
* The dynamic portion of the hook name, `$transient`, refers to the transient name.