update_option

Definition:
function update_option( $option, $newvalue ) {}

Update 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 can not be serialized or added as an option.

Parameters

  • string $option: Option name. Expected to not be SQL-escaped.
  • mixed $newvalue: Option value. Expected to not be SQL-escaped.

Return values

returns:False if value was not updated and true if value was updated.

Defined filters

  • pre_update_option_$option
    apply_filters( 'pre_update_option_' . $option, $newvalue, $oldvalue )

Defined actions

  • update_option
    do_action( 'update_option', $option, $oldvalue, $_newvalue );
  • update_option_{$option}
    do_action( "update_option_{$option}", $oldvalue, $_newvalue );
  • updated_option
    do_action( 'updated_option', $option, $oldvalue, $_newvalue );

Source code

function update_option( $option, $newvalue ) {

	global $wpdb;



	$option = trim($option);

	if ( empty($option) )

		return false;



	wp_protect_special_option( $option );



	if ( is_object($newvalue) )

		$newvalue = clone $newvalue;



	$newvalue = sanitize_option( $option, $newvalue );

	$oldvalue = get_option( $option );

	$newvalue = apply_filters( 'pre_update_option_' . $option, $newvalue, $oldvalue );



	// If the new and old values are the same, no need to update.

	if ( $newvalue === $oldvalue )

		return false;



	if ( false === $oldvalue )

		return add_option( $option, $newvalue );



	$notoptions = wp_cache_get( 'notoptions', 'options' );

	if ( is_array( $notoptions ) && isset( $notoptions[$option] ) ) {

		unset( $notoptions[$option] );

		wp_cache_set( 'notoptions', $notoptions, 'options' );

	}



	$_newvalue = $newvalue;

	$newvalue = maybe_serialize( $newvalue );



	do_action( 'update_option', $option, $oldvalue, $_newvalue );

	if ( ! defined( 'WP_INSTALLING' ) ) {

		$alloptions = wp_load_alloptions();

		if ( isset( $alloptions[$option] ) ) {

			$alloptions[$option] = $_newvalue;

			wp_cache_set( 'alloptions', $alloptions, 'options' );

		} else {

			wp_cache_set( $option, $_newvalue, 'options' );

		}

	}



	$result = $wpdb->update( $wpdb->options, array( 'option_value' => $newvalue ), array( 'option_name' => $option ) );



	if ( $result ) {

		do_action( "update_option_{$option}", $oldvalue, $_newvalue );

		do_action( 'updated_option', $option, $oldvalue, $_newvalue );

		return true;

	}

3185

No comments yet... Be the first to leave a reply!

Leave a comment