WordPress.org

Codex

Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

Function Reference/update option

Description

Use the function update_option() to update a named option/value pair to the options database table.

This function may be used in place of add_option, although it is not as flexible. update_option will check to see if the option already exists. If it does not, it will be added with add_option('option_name', 'option_value'). Unless you need to specify the optional arguments of add_option(), update_option() is a useful catch-all for both adding and updating options.

Usage

<?php update_option$option$new_value$autoload ); ?>

Parameters

$option
(string) (required) Name of the option to update. Must not exceed 64 characters. A list of valid default options to update can be found at the Option Reference.
Default: None
$newvalue
(mixed) (required) The NEW value for this option name. This value can be an integer, string, array, or object.
Default: None
$autoload
(mixed) (optional) Whether to load the option when WordPress starts up. For existing options `$autoload` can only be updated using `update_option()` if `$value` is also changed. Accepts 'yes' or true to enable, 'no' or false to disable. For non-existent options, the default value is 'yes'.
Default: null

Return Value

(boolean) 
True if option value has changed, false if not or if update failed.

Example

Updating Core Options

Set the default comment status to 'closed':

update_option( 'default_comment_status', 'closed' );

This option is usually set by from the Settings > Discussion administration panel. See the Option Reference for a full list of options used by WordPress Core.

Updating Custom Options

You can also create your own custom options. To update the option 'myhack_extraction_length' with the value 255, we would do this:

update_option( 'myhack_extraction_length', 255 );

This will automatically add the option if it does not exist. However, if we don't want this option to be autoloaded, we have to add it with add_option(). In this example, we update the option if it already exists, and if it does not exist we use add_option() and set $autoload to "no".

<?php

$option_name = 'myhack_extraction_length' ;
$new_value = '255' ;

if ( get_option( $option_name ) !== false ) {

    // The option already exists, so we just update it.
    update_option( $option_name, $new_value );

} else {

    // The option hasn't been added yet. We'll add it with $autoload set to 'no'.
    $deprecated = null;
    $autoload = 'no';
    add_option( $option_name, $new_value, $deprecated, $autoload );
}

?>

Notes

Upon getting an option in code, this option value will be cached. If you modify any options outside of the Options API, then try to update an option that's already cached, this will fail updating and return false. You can use the following method to clear the options cache before trying to get or update the options on the same request:

<?php
wp_cache_delete ( 'alloptions', 'options' );
?>

Change Log

Source File

update_option() is located in wp-includes/option.php.

Related

See also index of Function Reference and index of Template Tags.