WP_REST_Meta_Fields::prepare_value() WordPress Method

The prepare_value() method is used to prepare a value for a REST response. This is a helper method that should be used by child classes when overriding the prepare_value() method.

WP_REST_Meta_Fields::prepare_value( mixed $value, WP_REST_Request $request, array $args ) #

Prepares a meta value for output.


Description

Default preparation for meta fields. Override by passing the prepare_callback in your show_in_rest options.


Top ↑

Parameters

$value

(mixed)(Required)Meta value from the database.

$request

(WP_REST_Request)(Required)Request object.

$args

(array)(Required)REST-specific options for the meta key.


Top ↑

Return

(mixed) Value prepared for output. If a non-JsonSerializable object, null.


Top ↑

Source

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

	public static function prepare_value( $value, $request, $args ) {
		if ( $args['single'] ) {
			$schema = $args['schema'];
		} else {
			$schema = $args['schema']['items'];
		}

		if ( '' === $value && in_array( $schema['type'], array( 'boolean', 'integer', 'number' ), true ) ) {
			$value = static::get_empty_value_for_type( $schema['type'] );
		}

		if ( is_wp_error( rest_validate_value_from_schema( $value, $schema ) ) ) {
			return null;
		}

		return rest_sanitize_value_from_schema( $value, $schema );
	}


Top ↑

Changelog

Changelog
VersionDescription
4.7.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.