函数文档

add_network_option()

💡 云策文档标注

概述

add_network_option() 函数用于在 WordPress 多站点网络中新增一个网络选项。它不会更新已存在的选项,并支持通过 Hook 进行值过滤和操作触发。

关键要点

  • 函数用于添加网络选项,若选项已存在则返回 false 而不更新。
  • 参数包括网络 ID(可为 null 以使用当前网络)、选项名称(非 SQL 转义)和选项值(非 SQL 转义)。
  • 返回值是布尔类型,成功添加返回 true,否则返回 false。
  • 在非多站点环境下,会回退到 add_option() 函数。
  • 支持 pre_add_site_option_{$option} 过滤器在添加前修改值,以及 add_site_option 和 add_site_option_{$option} 动作在添加后触发。

代码示例

function add_network_option( $network_id, $option, $value ) {
    global $wpdb;

    if ( $network_id && ! is_numeric( $network_id ) ) {
        return false;
    }

    $network_id = (int) $network_id;

    // Fallback to the current network if a network ID is not specified.
    if ( ! $network_id ) {
        $network_id = get_current_network_id();
    }

    wp_protect_special_option( $option );

    $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 );
    } else {
        $cache_key = "$network_id:$option";

        $notoptions = wp_cache_get( $notoptions_key, 'site-options' );

        if ( ! is_array( $notoptions ) || ! isset( $notoptions[ $option ] ) ) {
            if ( false !== get_network_option( $network_id, $option, false ) ) {
                return false;
            }
        }

        $value = sanitize_option( $option, $value );

        $serialized_value = maybe_serialize( $value );
        $result           = $wpdb->insert(
            $wpdb->sitemeta,
            array(
                'site_id'    => $network_id,
                'meta_key'   => $option,
                'meta_value' => $serialized_value,
            )
        );

        if ( ! $result ) {
            return false;
        }

        wp_cache_set( $cache_key, $value, 'site-options' );

        $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 ( $result ) {
        do_action( "add_site_option_{$option}", $option, $value, $network_id );
        do_action( 'add_site_option', $option, $value, $network_id );
        return true;
    }

    return false;
}

注意事项

  • 选项名称和值不应进行 SQL 转义,函数内部会处理。
  • 在多站点环境中,选项存储在 sitemeta 表中,并利用缓存优化性能。
  • 使用 wp_protect_special_option() 保护特殊选项,避免意外修改。

📄 原文内容

Adds a new network option.

Description

Existing options will not be updated.

See also

Parameters

$network_idint|nullrequired
ID of the network. Can be null to default to the current network ID.
$optionstringrequired
Name of the option to add. Expected to not be SQL-escaped.
$valuemixedrequired
Option value, can be anything. Expected to not be SQL-escaped.

Return

bool True if the option was added, false otherwise.

Source

function add_network_option( $network_id, $option, $value ) {
	global $wpdb;

	if ( $network_id && ! is_numeric( $network_id ) ) {
		return false;
	}

	$network_id = (int) $network_id;

	// Fallback to the current network if a network ID is not specified.
	if ( ! $network_id ) {
		$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 3.0.0
	 * @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 );
	} else {
		$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 ) ) {
				return false;
			}
		}

		$value = sanitize_option( $option, $value );

		$serialized_value = maybe_serialize( $value );
		$result           = $wpdb->insert(
			$wpdb->sitemeta,
			array(
				'site_id'    => $network_id,
				'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 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 3.0.0
		 * @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 3.0.0
		 * @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 );

		return true;
	}

	return false;
}

Hooks

do_action( ‘add_site_option’, string $option, mixed $value, int $network_id )

Fires after a network option has been successfully added.

do_action( “add_site_option_{$option}”, string $option, mixed $value, int $network_id )

Fires after a specific network option has been successfully added.

apply_filters( “pre_add_site_option_{$option}”, mixed $value, string $option, int $network_id )

Filters the value of a specific network option before it is added.

Changelog

Version Description
4.4.0 Introduced.