WP_REST_Meta_Fields::prepare_value_for_response() WordPress Method

The WP_REST_Meta_Fields::prepare_value_for_response() method is used to prepare a value for output in a JSON response. This is typically used to format a value for display, or to convert a value from one type to another.

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

Prepares a meta value for a response.


Description

This is required because some native types cannot be stored correctly in the database, such as booleans. We need to cast back to the relevant type before passing back to JSON.


Top ↑

Parameters

$value

(mixed)(Required)Meta value to prepare.

$request

(WP_REST_Request)(Required)Current request object.

$args

(array)(Required)Options for the field.


Top ↑

Return

(mixed) Prepared value.


Top ↑

Source

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

	protected function prepare_value_for_response( $value, $request, $args ) {
		if ( ! empty( $args['prepare_callback'] ) ) {
			$value = call_user_func( $args['prepare_callback'], $value, $request, $args );
		}

		return $value;
	}


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.