rest_ensure_response( WP_HTTP_Response|WP_Error|mixed $response )

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


Description Description

This implements WP_HTTP_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.


Top ↑

Parameters Parameters

$response

(WP_HTTP_Response|WP_Error|mixed) (Required) Response to check.


Top ↑

Return Return

(WP_REST_Response|mixed) If response generated an error, WP_Error, if response is already an instance, WP_HTTP_Response, otherwise returns a new WP_REST_Response instance.


Top ↑

Source Source

File: wp-includes/rest-api.php

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

	if ( $response instanceof WP_HTTP_Response ) {
		return $response;
	}

	return new WP_REST_Response( $response );
}


Top ↑

Changelog Changelog

Changelog
Version Description
4.4.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.