Class: WP_REST_Response

Extends WP_HTTP_Response

Core class used to implement a REST response object.


Properties

Name Type(s) Default Value Summary
links array Links related to the response.
matched_route string The route that was to create the response.
matched_handler null | array The handler that was used to create the response.
data mixed Inherited ⋅ Response data.
headers array Inherited ⋅ Response headers.
status integer Inherited ⋅ Response status.

Methods

WP_REST_Response:: __construct( mixed $data = null, integer $status = 200, array $headers = array() )

Constructor. Since 4.4.0.

Arguments

Name Type(s) Default Value Description
$data mixed

Response data. Default null.

$status integer

Optional. HTTP status code. Default 200.

$headers array

Optional. HTTP header map. Default empty array.


Adds multiple links to the response. Since 4.4.0.

Link data should be an associative array with link relation as the key. The value can either be an associative array of link attributes (including href with the URL for the response), or a list of these associative arrays.

Arguments

Name Type(s) Default Value Description
$links array

Map of link relation to list of links.


WP_REST_Response:: as_error( )

Retrieves a WP_Error object from the response. Since 4.4.0.

Returns

WP_Error | null

WP_Error or null on not an errored response.


WP_REST_Response:: get_curies( )

Retrieves the CURIEs (compact URIs) used for relations. Since 4.5.0.

Returns

array

Compact URIs.


WP_REST_Response:: get_data( )

Retrieves the response data. Since 4.4.0.

Returns

mixed

Response data.


WP_REST_Response:: get_headers( )

Retrieves headers associated with the response. Since 4.4.0.

Returns

array

Map of header name to header value.


Retrieves links for the response. Since 4.4.0.

Returns

array

List of links.


WP_REST_Response:: get_matched_handler( )

Retrieves the handler that was used to generate the response. Since 4.4.0.

Returns

null | array

The handler that was used to create the response.


WP_REST_Response:: get_matched_route( )

Retrieves the route that was used. Since 4.4.0.

Returns

string

The matched route.


WP_REST_Response:: get_status( )

Retrieves the HTTP return code for the response. Since 4.4.0.

Returns

integer

The 3-digit HTTP status code.


WP_REST_Response:: header( string $key, string $value, boolean $replace = true )

Sets a single HTTP header. Since 4.4.0.

Arguments

Name Type(s) Default Value Description
$key string

Header name.

$value string

Header value.

$replace boolean

Optional. Whether to replace an existing header of the same name. Default true.


WP_REST_Response:: is_error( )

Checks if the response is an error, i.e. >= 400 response code. Since 4.4.0.

Returns

boolean

Whether the response is an error.


WP_REST_Response:: jsonSerialize( )

Retrieves the response data for JSON serialization. Since 4.4.0.

It is expected that in most implementations, this will return the same as get_data(), however this may be different if you want to do custom JSON data handling.

Returns

mixed

Any JSON-serializable value.


Removes a link from the response. Since 4.4.0.

Arguments

Name Type(s) Default Value Description
$rel string

Link relation. Either an IANA registered type, or an absolute URL.

$href string

Optional. Only remove links for the relation matching the given href. Default null.


WP_REST_Response:: set_data( mixed $data )

Sets the response data. Since 4.4.0.

Arguments

Name Type(s) Default Value Description
$data mixed

Response data.


WP_REST_Response:: set_headers( array $headers )

Sets all header values. Since 4.4.0.

Arguments

Name Type(s) Default Value Description
$headers array

Map of header name to header value.


WP_REST_Response:: set_matched_handler( array $handler )

Sets the handler that was responsible for generating the response. Since 4.4.0.

Arguments

Name Type(s) Default Value Description
$handler array

The matched handler.


WP_REST_Response:: set_matched_route( string $route )

Sets the route (regex for path) that caused the response. Since 4.4.0.

Arguments

Name Type(s) Default Value Description
$route string

Route name.


WP_REST_Response:: set_status( integer $code )

Sets the 3-digit HTTP status code. Since 4.4.0.

Arguments

Name Type(s) Default Value Description
$code integer

HTTP status.


WordPress Developer Newsletter

Stay informed of new chapter releases, important WordPress API updates and more.