WordPress at a glance
Home All Function Functions APIs REST API

rest_ensure_response() WP 1.0

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

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.

No Hooks.

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.

Usage

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

Code of rest ensure response: wp-includes/rest-api.php WP 5.2.2

<?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 );
}

Related Functions

From tag: REST API

No comments
    Hello, !     Log In . Register