WordPress at a glance

rest_ensure_response() WP 1.0

Ensures a REST response is a response object (for consistency).

This implements WP_REST_Response, allowing usage of set_status/header/etc without needing to double-check the object. Will also allow WP_Error to indicate error responses, so users should immediately check for this value.


WP_REST_Response/WP_Error. If response generated an error, WP_Error, if response is already an instance, WP_REST_Response, otherwise returns a new WP_REST_Response instance.

Works based on: WP_REST_Response()

No Hooks.


rest_ensure_response( $response );
$response(WP_REST_Response/WP_Error/WP_HTTP_Response/mixed) (required)
Response to check.

Список изменений

С версии 4.4.0 Введена.

Code of rest_ensure_response() WP 5.5.1

function rest_ensure_response( $response ) {
	if ( is_wp_error( $response ) ) {
		return $response;

	if ( $response instanceof WP_REST_Response ) {
		return $response;

	// While WP_HTTP_Response is the base class of WP_REST_Response, it doesn't provide
	// all the required methods used in WP_REST_Server::dispatch().
	if ( $response instanceof WP_HTTP_Response ) {
		return new WP_REST_Response(

	return new WP_REST_Response( $response );

Related Functions

From tag: REST API

No comments
    Log In . Register