update_option() – Updates the value of an option that was already added.

You appear to be a bot. Output may be restricted

Description

Updates the value of an option that was already added.

You do not need to serialize values. If the value needs to be serialized, then it will be serialized before it is inserted into the database. Remember, resources cannot be serialized or added as an option. If the option does not exist, it will be created. This function is designed to work with or without a logged-in user. In terms of security, plugin developers should check the current user's capabilities before updating any options.

Usage

$bool = update_option( $option, $value, $autoload );

Parameters

$option
( string ) required – Name of the option to update. Expected to not be SQL-escaped.
$value
( mixed ) required – Option value. Must be serializable if non-scalar. Expected to not be SQL-escaped.
$autoload
( string|bool ) optional – 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'|true to enable or 'no'|false to disable. Autoloading too many options can lead to performance problems, especially if the options are not frequently used. For options which are accessed across several places in the frontend, it is recommended to autoload them, by using 'yes'|true. For options which are accessed only on few specific URLs, it is recommended to not autoload them, by using 'no'|false. For non-existent options, the default value is 'yes'. Default null.

Returns

bool True if the value was updated, false otherwise.

Source

File name: wordpress/wp-includes/option.php
Lines:

1 to 100 of 180
function update_option( $option, $value, $autoload = null ) {
  global $wpdb;

  if ( is_scalar( $option ) ) {
    $option = trim( $option );
  }

  if ( empty( $option ) ) {
    return false;
  }

  /*
	 * Until a proper _deprecated_option() function can be introduced,
	 * redirect requests to deprecated keys to the new, correct ones.
	 */
  $deprecated_keys = array(
    'blacklist_keys'    => 'disallowed_keys',
    'comment_whitelist' => 'comment_previously_approved',
  );

  if ( isset( $deprecated_keys[ $option ] ) && ! wp_installing() ) {
    _deprecated_argument(
      update_option,
      '5.5.0',
      sprintf(
        /* translators: 1: Deprecated option key, 2: New option key. */
        __( 'The "%1$s" option key has been renamed to "%2$s".' ),
        $option,
        $deprecated_keys[ $option ]
      )
    );
    return update_option( $deprecated_keys[ $option ], $value, $autoload );
  }

  wp_protect_special_option( $option );

  if ( is_object( $value ) ) {
    $value = clone $value;
  }

  $value     = sanitize_option( $option, $value );
  $old_value = get_option( $option );

  
/**
 * Filters a specific option before its value is (maybe) serialized and updated.
 *
 * The dynamic portion of the hook name, `$option`, refers to the option name.
 *
 * @since 2.6.0
 * @since 4.4.0 The `$option` parameter was added.
 *
 * @param mixed  $value     The new, unserialized option value.
 * @param mixed  $old_value The old option value.
 * @param string $option    Option name.
 */
  $value = apply_filters( "pre_update_option_{$option}", $value, $old_value, $option );

  
/**
 * Filters an option before its value is (maybe) serialized and updated.
 *
 * @since 3.9.0
 *
 * @param mixed  $value     The new, unserialized option value.
 * @param string $option    Name of the option.
 * @param mixed  $old_value The old option value.
 */
  $value = apply_filters( 'pre_update_option', $value, $option, $old_value );

  /*
	 * If the new and old values are the same, no need to update.
	 *
	 * Unserialized values will be adequate in most cases. If the unserialized
	 * data differs, the (maybe) serialized data is checked to avoid
	 * unnecessary database calls for otherwise identical object instances.
	 *
	 * See https://core.trac.wordpress.org/ticket/38903
	 */
  if ( $value === $old_value || maybe_serialize( $value ) === maybe_serialize( $old_value ) ) {
    return false;
  }

  
/** This filter is documented in wp-includes/option.php */
  if ( apply_filters( "default_option_{$option}", false, $option, false ) === $old_value ) {
    // Default setting for new options is 'yes'.
    if ( null === $autoload ) {
      $autoload = 'yes';
    }

    return add_option( $option, $value, '', $autoload );
  }

  $serialized_value = maybe_serialize( $value );

  
/**
 * Fires immediately before an option value is updated.
 *
 * @since 2.9.0
 *
 * @param string $option    Name of the option to update.
 * @param mixed  $old_value The old option value.
 

 View on GitHub View on Trac