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