add_site_option

The timeline below displays how wordpress function add_site_option has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 5.5

/**
 * Adds a new option for the current network.
 *
 * Existing options will not be updated. Note that prior to 3.3 this wasn't the case.
 *
 * @since 2.8.0
 * @since 4.4.0 Modified into wrapper for add_network_option()
 *
 * @see add_network_option()
 *
 * @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_site_option($option, $value)
{
    return add_network_option(null, $option, $value);
}

WordPress Version: 5.4

/**
 * Adds a new option for the current network.
 *
 * Existing options will not be updated. Note that prior to 3.3 this wasn't the case.
 *
 * @since 2.8.0
 * @since 4.4.0 Modified into wrapper for add_network_option()
 *
 * @see add_network_option()
 *
 * @param string $option Name of 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 False if the option was not added. True if the option was added.
 */
function add_site_option($option, $value)
{
    return add_network_option(null, $option, $value);
}

WordPress Version: 4.4

/**
 * Add a new option for the current network.
 *
 * Existing options will not be updated. Note that prior to 3.3 this wasn't the case.
 *
 * @since 2.8.0
 * @since 4.4.0 Modified into wrapper for add_network_option()
 *
 * @see add_network_option()
 *
 * @param string $option Name of 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 False if the option was not added. True if the option was added.
 */
function add_site_option($option, $value)
{
    return add_network_option(null, $option, $value);
}

WordPress Version: 4.3

/**
 * Add a new site option.
 *
 * Existing options will not be updated. Note that prior to 3.3 this wasn't the case.
 *
 * @since 2.8.0
 *
 * @see add_option()
 *
 * @global wpdb $wpdb
 *
 * @param string $option Name of option to add. Expected to not be SQL-escaped.
 * @param mixed  $value  Optional. Option value, can be anything. Expected to not be SQL-escaped.
 * @return bool False if option was not added and true if option was added.
 */
function add_site_option($option, $value)
{
    global $wpdb;
    wp_protect_special_option($option);
    /**
     * Filter the value of a specific site 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 3.0.0
     *
     * @param mixed $value Value of site option.
     */
    $value = apply_filters('pre_add_site_option_' . $option, $value);
    $notoptions_key = "{$wpdb->siteid}:notoptions";
    if (!is_multisite()) {
        $result = add_option($option, $value);
    } else {
        $cache_key = "{$wpdb->siteid}:{$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_site_option($option)) {
                return false;
            }
        }
        $value = sanitize_option($option, $value);
        $serialized_value = maybe_serialize($value);
        $result = $wpdb->insert($wpdb->sitemeta, array('site_id' => $wpdb->siteid, 'meta_key' => $option, 'meta_value' => $serialized_value));
        if (!$result) {
            return false;
        }
        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');
        }
    }
    if ($result) {
        /**
         * Fires after a specific site 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 3.0.0
         *
         * @param string $option Name of site option.
         * @param mixed  $value  Value of site option.
         */
        do_action("add_site_option_{$option}", $option, $value);
        /**
         * Fires after a site option has been successfully added.
         *
         * @since 3.0.0
         *
         * @param string $option Name of site option.
         * @param mixed  $value  Value of site option.
         */
        do_action("add_site_option", $option, $value);
        return true;
    }
    return false;
}

WordPress Version: 4.1

/**
 * Add a new site option.
 *
 * Existing options will not be updated. Note that prior to 3.3 this wasn't the case.
 *
 * @since 2.8.0
 *
 * @see add_option()
 *
 * @param string $option Name of option to add. Expected to not be SQL-escaped.
 * @param mixed $value Optional. Option value, can be anything. Expected to not be SQL-escaped.
 * @return bool False if option was not added and true if option was added.
 */
function add_site_option($option, $value)
{
    global $wpdb;
    wp_protect_special_option($option);
    /**
     * Filter the value of a specific site 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 3.0.0
     *
     * @param mixed $value Value of site option.
     */
    $value = apply_filters('pre_add_site_option_' . $option, $value);
    $notoptions_key = "{$wpdb->siteid}:notoptions";
    if (!is_multisite()) {
        $result = add_option($option, $value);
    } else {
        $cache_key = "{$wpdb->siteid}:{$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_site_option($option)) {
                return false;
            }
        }
        $value = sanitize_option($option, $value);
        $serialized_value = maybe_serialize($value);
        $result = $wpdb->insert($wpdb->sitemeta, array('site_id' => $wpdb->siteid, 'meta_key' => $option, 'meta_value' => $serialized_value));
        if (!$result) {
            return false;
        }
        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');
        }
    }
    if ($result) {
        /**
         * Fires after a specific site 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 3.0.0
         *
         * @param string $option Name of site option.
         * @param mixed  $value  Value of site option.
         */
        do_action("add_site_option_{$option}", $option, $value);
        /**
         * Fires after a site option has been successfully added.
         *
         * @since 3.0.0
         *
         * @param string $option Name of site option.
         * @param mixed  $value  Value of site option.
         */
        do_action("add_site_option", $option, $value);
        return true;
    }
    return false;
}

WordPress Version: 3.9

/**
 * Add a new site option.
 *
 * Existing options will not be updated. Note that prior to 3.3 this wasn't the case.
 *
 * @since 2.8.0
 *
 * @see add_option()
 *
 * @param string $option Name of option to add. Expected to not be SQL-escaped.
 * @param mixed $value Optional. Option value, can be anything. Expected to not be SQL-escaped.
 * @return bool False if option was not added and true if option was added.
 */
function add_site_option($option, $value)
{
    global $wpdb;
    wp_protect_special_option($option);
    /**
     * Filter the value of a specific site 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 3.0.0
     *
     * @param mixed $value Value of site option.
     */
    $value = apply_filters('pre_add_site_option_' . $option, $value);
    $notoptions_key = "{$wpdb->siteid}:notoptions";
    if (!is_multisite()) {
        $result = add_option($option, $value);
    } else {
        $cache_key = "{$wpdb->siteid}:{$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_site_option($option)) {
                return false;
            }
        }
        $value = sanitize_option($option, $value);
        $serialized_value = maybe_serialize($value);
        $result = $wpdb->insert($wpdb->sitemeta, array('site_id' => $wpdb->siteid, 'meta_key' => $option, 'meta_value' => $serialized_value));
        if (!$result) {
            return false;
        }
        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');
        }
    }
    if ($result) {
        /**
         * Fires after a specific site 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 3.0.0
         *
         * @param string $option Name of site option.
         * @param mixed  $value  Value of site option.
         */
        do_action("add_site_option_{$option}", $option, $value);
        /**
         * Fires after a site option has been successfully added.
         *
         * @since 3.0.0
         *
         * @param string $option Name of site option.
         * @param mixed  $value  Value of site option.
         */
        do_action("add_site_option", $option, $value);
        return true;
    }
    return false;
}

WordPress Version: .10

/**
 * Add a new site option.
 *
 * Existing options will not be updated. Note that prior to 3.3 this wasn't the case.
 *
 * @see add_option()
 * @package WordPress
 * @subpackage Option
 * @since 2.8.0
 *
 * @uses apply_filters() Calls 'pre_add_site_option_$option' hook to allow overwriting the
 * 	option value to be stored.
 * @uses do_action() Calls 'add_site_option_$option' and 'add_site_option' hooks on success.
 *
 * @param string $option Name of option to add. Expected to not be SQL-escaped.
 * @param mixed $value Optional. Option value, can be anything. Expected to not be SQL-escaped.
 * @return bool False if option was not added and true if option was added.
 */
function add_site_option($option, $value)
{
    global $wpdb;
    wp_protect_special_option($option);
    $value = apply_filters('pre_add_site_option_' . $option, $value);
    $notoptions_key = "{$wpdb->siteid}:notoptions";
    if (!is_multisite()) {
        $result = add_option($option, $value);
    } else {
        $cache_key = "{$wpdb->siteid}:{$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_site_option($option)) {
                return false;
            }
        }
        $value = sanitize_option($option, $value);
        $serialized_value = maybe_serialize($value);
        $result = $wpdb->insert($wpdb->sitemeta, array('site_id' => $wpdb->siteid, 'meta_key' => $option, 'meta_value' => $serialized_value));
        if (!$result) {
            return false;
        }
        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');
        }
    }
    if ($result) {
        do_action("add_site_option_{$option}", $option, $value);
        do_action("add_site_option", $option, $value);
        return true;
    }
    return false;
}

WordPress Version: 3.7

/**
 * Add a new site option.
 *
 * Existing options will not be updated. Note that prior to 3.3 this wasn't the case.
 *
 * @see add_option()
 * @package WordPress
 * @subpackage Option
 * @since 2.8.0
 *
 * @uses apply_filters() Calls 'pre_add_site_option_$option' hook to allow overwriting the
 * 	option value to be stored.
 * @uses do_action() Calls 'add_site_option_$option' and 'add_site_option' hooks on success.
 *
 * @param string $option Name of option to add. Expected to not be SQL-escaped.
 * @param mixed $value Optional. Option value, can be anything. Expected to not be SQL-escaped.
 * @return bool False if option was not added and true if option was added.
 */
function add_site_option($option, $value)
{
    global $wpdb;
    wp_protect_special_option($option);
    $value = apply_filters('pre_add_site_option_' . $option, $value);
    if (!is_multisite()) {
        $result = add_option($option, $value);
    } else {
        $cache_key = "{$wpdb->siteid}:{$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', 'site-options');
        if (!is_array($notoptions) || !isset($notoptions[$option])) {
            if (false !== get_site_option($option)) {
                return false;
            }
        }
        $value = sanitize_option($option, $value);
        $serialized_value = maybe_serialize($value);
        $result = $wpdb->insert($wpdb->sitemeta, array('site_id' => $wpdb->siteid, 'meta_key' => $option, 'meta_value' => $serialized_value));
        if (!$result) {
            return false;
        }
        wp_cache_set($cache_key, $value, 'site-options');
        // This option exists now
        $notoptions = wp_cache_get('notoptions', 'site-options');
        // yes, again... we need it to be fresh
        if (is_array($notoptions) && isset($notoptions[$option])) {
            unset($notoptions[$option]);
            wp_cache_set('notoptions', $notoptions, 'site-options');
        }
    }
    if ($result) {
        do_action("add_site_option_{$option}", $option, $value);
        do_action("add_site_option", $option, $value);
        return true;
    }
    return false;
}