Codex

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_name value is escaped with $wpdb->escape before the INSERT statement.

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. 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 a string, an array, an object or a serialized value.
Default: None

Return Value

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

Option Values

View the Option_Reference section.

Example

Update the option name myhack_extraction_length with the value 255. If the option does not exist then use add_option and set autoload to no.

<?php
$option_name = 'myhack_extraction_length' ;
$new_value = '255' ;

if ( get_option( $option_name ) != $new_value ) {
    update_option( $option_name, $new_value );
} else {
    $deprecated = ' ';
    $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.
This article is marked as in need of editing. You can help Codex by editing it.