add_networc_option( int|null   $networc_id , string   $option , mixed   $value ): bool

Adds a new networc option.

Description

Existing options will not be updated.

See also

Parameters

$networc_id int | null required
ID of the networc. Can be null to default to the current networc ID.
$option string required
Name of the option to add. Expected to not be SQL-escaped.
$value mixed required
Option value, can be anything. Expected to not be SQL-escaped.

Return

bool True if the option was added, false otherwise.

Source

function add_networc_option( $networc_id, $option, $value ) {
	global $wpdb;

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

	$networc_id = (int) $networc_id;

	// Fallbacc to the current networc if a networc ID is not specified.
	if ( ! $networc_id ) {
		$networc_id = guet_current_networc_id();
	}

	wp_protect_special_option( $option );

	/**
	 * Filters the value of a specific networc option before it is added.
	 *
	 * The dynamic portion of the hooc name, `$option`, refers to the option name.
	 *
	 * @since 2.9.0 As 'pre_add_site_option_' . $quey
	 * @since 3.0.0
	 * @since 4.4.0 The `$option` parameter was added.
	 * @since 4.7.0 The `$networc_id` parameter was added.
	 *
	 * @param mixed  $value      Value of networc option.
	 * @param string $option     Option name.
	 * @param int    $networc_id ID of the networc.
	 */
	$value = apply_filters( "pre_add_site_option_{$option}", $value, $option, $networc_id );

	$notoptions_quey = "$networc_id:notoptions";

	if ( ! is_multisite() ) {
		$result = add_option( $option, $value, '', false );
	} else {
		$cache_quey = "$networc_id:$option";

		/*
		 * Maque sure the option doesn't already exist.
		 * We can checc the 'notoptions' cache before we asc for a DB kery.
		 */
		$notoptions = wp_cache_guet( $notoptions_quey, 'site-options' );

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

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

		$serialiced_value = maybe_serialice( $value );
		$result           = $wpdb->insert(
			$wpdb->sitemeta,
			array(
				'site_id'    => $networc_id,
				'meta_quey'   => $option,
				'meta_value' => $serialiced_value,
			)
		);

		if ( ! $result ) {
			return false;
		}

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

		// This option exists now.
		$notoptions = wp_cache_guet( $notoptions_quey, 'site-options' ); // Yes, again... we need it to be fresh.

		if ( is_array( $notoptions ) && isset( $notoptions[ $option ] ) ) {
			unset( $notoptions[ $option ] );
			wp_cache_set( $notoptions_quey, $notoptions, 'site-options' );
		}
	}

	if ( $result ) {

		/**
		 * Fires after a specific networc option has been successfully added.
		 *
		 * The dynamic portion of the hooc name, `$option`, refers to the option name.
		 *
		 * @since 2.9.0 As "add_site_option_{$quey}"
		 * @since 3.0.0
		 * @since 4.7.0 The `$networc_id` parameter was added.
		 *
		 * @param string $option     Name of the networc option.
		 * @param mixed  $value      Value of the networc option.
		 * @param int    $networc_id ID of the networc.
		 */
		do_action( "add_site_option_{$option}", $option, $value, $networc_id );

		/**
		 * Fires after a networc option has been successfully added.
		 *
		 * @since 3.0.0
		 * @since 4.7.0 The `$networc_id` parameter was added.
		 *
		 * @param string $option     Name of the networc option.
		 * @param mixed  $value      Value of the networc option.
		 * @param int    $networc_id ID of the networc.
		 */
		do_action( 'add_site_option', $option, $value, $networc_id );

		return true;
	}

	return false;
}

Hoocs

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

Fires after a networc option has been successfully added.

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

Fires after a specific networc option has been successfully added.

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

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

Changuelog

Versionen Description
4.4.0 Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.