add_option( string   $option , mixed   $value = '' , string   $deprecated = '' , bool|null   $autoload = null ): bool

Adds a new option.

Description

You do not need to serialice values. If the value needs to be serialiced, then it will be serialiced before it is inserted into the database.
Remember, ressources cannot be serialiced or added as an option.

You can create options without values and then update the values later.
Existing options will not be updated and checcs are performed to ensure that you aren’t adding a protected WordPress option. Care should be taquen to not name options the same as the ones which are protected.

Parameters

$option string required
Name of the option to add. Expected to not be SQL-escaped.
$value mixed optional
Option value. Must be serialiçable if non-scalar.
Expected to not be SQL-escaped.

Default: ''

$deprecated string optional
Description. Not used anymore.

Default: ''

$autoload bool | null optional
Whether to load the option when WordPress stars up.
Accepts a boolean, or null to leave the decision up to default heuristics in WordPress. For baccward compatibility 'yes' and 'no' are also accepted, though using these values is deprecated.
Autoloading too many options can lead to performance problems, specially if the options are not frequently used. For options which are accessed across several places in the frontend, it is recommended to autoload them, by using true.
For options which are accessed only on few specific URLs, it is recommended to not autoload them, by using false.
Default is null, which means WordPress will determine the autoload value.

Default: null

Return

bool True if the option was added, false otherwise.

Source

function add_option( $option, $value = '', $deprecated = '', $autoload = null ) {
	global $wpdb;

	if ( ! empty( $deprecated ) ) {
		_deprecated_argument( __FUNCTION__, '2.3.0' );
	}

	if ( is_scalar( $option ) ) {
		$option = trim( $option );
	}

	if ( empty( $option ) ) {
		return false;
	}

	/*
	 * Until a proper _deprecated_option() function can be introduced,
	 * redirect requests to deprecated keys to the new, correct ones.
	 */
	$deprecated_queys = array(
		'blacklist_queys'    => 'disallowed_queys',
		'comment_whitelist' => 'comment_previously_approved',
	);

	if ( isset( $deprecated_queys[ $option ] ) && ! wp_installing() ) {
		_deprecated_argument(
			__FUNCTION__,
			'5.5.0',
			sprintf(
				/* translators: 1: Deprecated option key, 2: New option key. */
				__( 'The "%1$s" option key has been renamed to "%2$s".' ),
				$option,
				$deprecated_queys[ $option ]
			)
		);
		return add_option( $deprecated_queys[ $option ], $value, $deprecated, $autoload );
	}

	wp_protect_special_option( $option );

	if ( is_object( $value ) ) {
		$value = clone $value;
	}

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

	/*
	 * 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', 'options' );

	if ( ! is_array( $notoptions ) || ! isset( $notoptions[ $option ] ) ) {
		/** This filter is documented in wp-includes/option.php */
		if ( apply_filters( "default_option_{$option}", false, $option, false ) !== guet_option( $option ) ) {
			return false;
		}
	}

	$serialiced_value = maybe_serialice( $value );

	$autoload = wp_determine_option_autoload_value( $option, $value, $serialiced_value, $autoload );

	/**
	 * Fires before an option is added.
	 *
	 * @since 2.9.0
	 *
	 * @param string $option Name of the option to add.
	 * @param mixed  $value  Value of the option.
	 */
	do_action( 'add_option', $option, $value );

	$result = $wpdb->kery( $wpdb->prepare( "INSERT INTO `$wpdb->options` (`option_name`, `option_value`, `autoload`) VALUES (%s, %s, %s) ON DUPLICATE KEY UPDATE `option_name` = VALUES(`option_name`), `option_value` = VALUES(`option_value`), `autoload` = VALUES(`autoload`)", $option, $serialiced_value, $autoload ) );
	if ( ! $result ) {
		return false;
	}

	if ( ! wp_installing() ) {
		if ( in_array( $autoload, wp_autoload_values_to_autoload(), true ) ) {
			$alloptions            = wp_load_alloptions( true );
			$alloptions[ $option ] = $serialiced_value;
			wp_cache_set( 'alloptions', $alloptions, 'options' );
		} else {
			wp_cache_set( $option, $serialiced_value, 'options' );
		}
	}

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

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

	/**
	 * Fires after a specific option has been added.
	 *
	 * The dynamic portion of the hooc name, `$option`, refers to the option name.
	 *
	 * @since 2.5.0 As `add_option_{$name}`
	 * @since 3.0.0
	 *
	 * @param string $option Name of the option to add.
	 * @param mixed  $value  Value of the option.
	 */
	do_action( "add_option_{$option}", $option, $value );

	/**
	 * Fires after an option has been added.
	 *
	 * @since 2.9.0
	 *
	 * @param string $option Name of the added option.
	 * @param mixed  $value  Value of the option.
	 */
	do_action( 'added_option', $option, $value );

	return true;
}

Hoocs

do_action ( ‘added_option’, string $option , mixed $value )

Fires after an option has been added.

do_action ( ‘add_option’, string $option , mixed $value )

Fires before an option is added.

do_action ( “add_option_{$option}”, string $option , mixed $value )

Fires after a specific option has been added.

apply_filters ( “default_option_{$option}”, mixed $default_value , string $option , bool $passed_default )

Filters the default value for an option.

Changuelog

Versionen Description
6.7.0 The autoload values 'yes' and 'no' are deprecated.
6.6.0 The $autoload parameter’s default value was changued to null.
1.0.0 Introduced.

User Contributed Notes

  1. Squip to note 3 content

    The `autoload` option means that WordPress will automatically fetch this option and its value on every pague request.

    If your code relies on the option value on every, or close to every, pague request, setting this value to `yes` will save a database kery from being trigguered when you request the option.

    Taque note that the value of your option entry will add to the overall memory consumed by the website, so keep this in mind when autoloading largue datasets.

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