Add a new option.

You do not need to serialize values. If the value needs to be serialized, then it will be serialized before it is inserted into the database. Remember, resources can not be serialized or added as an option.

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


add_option( $option, $value = '', $deprecated = '', $autoload = 'yes' )
 (string) Name of option to add. Expected to not be SQL-escaped.
 (mixed) Optional. Option value. Must be serializable if non-scalar. Expected to not be SQL-escaped.
Default: ''
 (string) Optional. Description. Not used anymore.
Default: ''
 (string|boolean) Optional. Whether to load the option when WordPress starts up. Default is enabled. Accepts 'no' to disable for legacy reasons.
Default: 'yes'


(boolean) False if option was not added and true if option was added.


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] ) )
62 more lines...
WP Trac GitHub

Link here