WP_REST_Meta_Fields::is_meta_value_same_as_stored_value() WordPress Method

The WP_REST_Meta_Fields::is_meta_value_same_as_stored_value() method is used to check if a given meta value is the same as the value currently stored in the database. This is useful for checking if a given meta value has been changed since the last time it was saved.

WP_REST_Meta_Fields::is_meta_value_same_as_stored_value( string $meta_key, string $subtype, mixed $stored_value, mixed $user_value ) #

Checks if the user provided value is equivalent to a stored value for the given meta key.


Parameters

$meta_key

(string)(Required)The meta key being checked.

$subtype

(string)(Required)The object subtype.

$stored_value

(mixed)(Required)The currently stored value retrieved from get_metadata().

$user_value

(mixed)(Required)The value provided by the user.


Top ↑

Return

(bool)


Top ↑

Source

File: wp-includes/rest-api/fields/class-wp-rest-meta-fields.php

	protected function is_meta_value_same_as_stored_value( $meta_key, $subtype, $stored_value, $user_value ) {
		$args      = $this->get_registered_fields()[ $meta_key ];
		$sanitized = sanitize_meta( $meta_key, $user_value, $this->get_meta_type(), $subtype );

		if ( in_array( $args['type'], array( 'string', 'number', 'integer', 'boolean' ), true ) ) {
			// The return value of get_metadata will always be a string for scalar types.
			$sanitized = (string) $sanitized;
		}

		return $sanitized === $stored_value;
	}


Top ↑

Changelog

Changelog
VersionDescription
5.5.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.