WP_REST_Server::response_to_data() WordPress Method

The WP_REST_Server::response_to_data() method is used to filter the response data for a request. This is useful for modifying the data before it is converted into the JSON format, or for adding extra data to the response that is not included in the original data.

WP_REST_Server::response_to_data( WP_REST_Response $response, bool|string[] $embed ) #

Converts a response to data to send.


Parameters

$response

(WP_REST_Response)(Required)Response object.

$embed

(bool|string[])(Required)Whether to embed all links, a filtered list of link relations, or no links.


Top ↑

Return

(array) Data with sub-requests embedded.

  • '_links'
    (array) Links.
  • '_embedded'
    (array) Embedded objects.


Top ↑

Source

File: wp-includes/rest-api/class-wp-rest-server.php

	public function response_to_data( $response, $embed ) {
		$data  = $response->get_data();
		$links = self::get_compact_response_links( $response );

		if ( ! empty( $links ) ) {
			// Convert links to part of the data.
			$data['_links'] = $links;
		}

		if ( $embed ) {
			$this->embed_cache = array();
			// Determine if this is a numeric array.
			if ( wp_is_numeric_array( $data ) ) {
				foreach ( $data as $key => $item ) {
					$data[ $key ] = $this->embed_links( $item, $embed );
				}
			} else {
				$data = $this->embed_links( $data, $embed );
			}
			$this->embed_cache = array();
		}

		return $data;
	}


Top ↑

Changelog

Changelog
VersionDescription
5.4.0The $embed parameter can now contain a list of link relations to include.
4.4.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.