添加新选项。

原型

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

描述

你不需要序列化值。如果值需要序列化,则在将其插入数据库之前将序列化。请记住,资源无法序列化或作为选项添加。

参数

$option (string) (Required) 要添加的选项的名称。预计不会被SQL转义。

$value (mixed) (Optional)  期权价值。如果是非标量,则必须可序列化。预计不会被SQL转义。

$deprecated (string) (Optional)  描述。不再使用了。

$autoload (string|bool) (Optional)  是否在WordPress启动时加载选项。默认值已启用。接受“否”以禁用遗留原因。

返回值

(bool)  如果未添加选项,则返回false,如果添加了选项,则返回true。

源文件

路径:wp-includes/option.php

<?php
...
function add_option( $option, $value = '', $deprecated = '', $autoload = 'yes' ) {
	global $wpdb;
	if ( !empty( $deprecated ) )
		_deprecated_argument( __FUNCTION__, '2.3.0' );
	$option = trim($option);
	if ( empty($option) )
		return false;
	wp_protect_special_option( $option );
	if ( is_object($value) )
		$value = clone $value;
	$value = sanitize_option( $option, $value );
	// 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', '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 ) !== get_option( $option ) )
			return false;
	$serialized_value = maybe_serialize( $value );
	$autoload = ( 'no' === $autoload || false === $autoload ) ? 'no' : 'yes';
	/**
	 * 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->query( $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, $serialized_value, $autoload ) );
	if ( ! $result )
		return false;
	if ( ! wp_installing() ) {
		if ( 'yes' == $autoload ) {
			$alloptions = wp_load_alloptions();
			$alloptions[ $option ] = $serialized_value;
			wp_cache_set( 'alloptions', $alloptions, 'options' );
		} else {
			wp_cache_set( $option, $serialized_value, 'options' );
		}
	}
	// This option exists now
	$notoptions = wp_cache_get( '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 hook 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;
}
...
?>

其他

英文文档:https://developer.wordpress.org/reference/functions/add_option/