WP_REST_Server::envelope_response() WordPress Method

The envelope_response() method is used to wrap the server response in a envelope. This is useful for cases where the response needs to be formatted differently than the default.

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

Wraps the response in an envelope.


Description

The enveloping technique is used to work around browser/client compatibility issues. Essentially, it converts the full HTTP response to data instead.


Top ↑

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

(WP_REST_Response) New response with wrapped data


Top ↑

Source

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

	public function envelope_response( $response, $embed ) {
		$envelope = array(
			'body'    => $this->response_to_data( $response, $embed ),
			'status'  => $response->get_status(),
			'headers' => $response->get_headers(),
		);

		/**
		 * Filters the enveloped form of a REST API response.
		 *
		 * @since 4.4.0
		 *
		 * @param array            $envelope {
		 *     Envelope data.
		 *
		 *     @type array $body    Response data.
		 *     @type int   $status  The 3-digit HTTP status code.
		 *     @type array $headers Map of header name to header value.
		 * }
		 * @param WP_REST_Response $response Original response data.
		 */
		$envelope = apply_filters( 'rest_envelope_response', $envelope, $response );

		// Ensure it's still a response and return.
		return rest_ensure_response( $envelope );
	}


Top ↑

Changelog

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