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.


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.


Return

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


Source

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

	public static function prepare_value( $value, $request, $args ) {
		$type = $args['schema']['type'];

		// For multi-value fields, check the item type instead.
		if ( 'array' === $type && ! empty( $args['schema']['items']['type'] ) ) {
			$type = $args['schema']['items']['type'];
		}

		switch ( $type ) {
			case 'string':
				$value = (string) $value;
				break;
			case 'integer':
				$value = (int) $value;
				break;
			case 'number':
				$value = (float) $value;
				break;
			case 'boolean':
				$value = (bool) $value;
				break;
		}

		// Don't allow objects to be output.
		if ( is_object( $value ) && ! ( $value instanceof JsonSerializable ) ) {
			return null;
		}

		return $value;
	}

Changelog

Changelog
Version Description
WP-4.7.0 Introduced.