WordPress.org

Ready to get started?Download WordPress

Codex

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

Function Reference/update option

Contents

Description

Use the function update_option() to update a named option/value pair to the options database table. The $option (option name) value is escaped with $wpdb->prepare before the INSERT statement but not the option value, this value should always be properly sanitized.

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.

Note: This function cannot be used to change whether an option is to be loaded (or not loaded) by wp_load_alloptions(). In that case, a delete_option() should be followed by use of the add_option() function.

Usage

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

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

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 );
}

?>

Change Log

Since: 1.0.0

Source File

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

Related

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