Languages: English • Русский • (Add your language)
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.
<?php update_option( $option, $new_value ); ?>
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.
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 );
}
?>
Since: 1.0.0
update_option() is located in wp-includes/option.php.