Ensures a REST response is a response object (for consistency).
Description
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.
698functionrest_ensure_response($response){699if(is_wp_error($response)){700return$response;701}702703if($responseinstanceofWP_REST_Response){704return$response;705}706707/*708 * While WP_HTTP_Response is the base class of WP_REST_Response, it doesn't provide709 * all the required methods used in WP_REST_Server::dispatch().710 */711if($responseinstanceofWP_HTTP_Response){712returnnewWP_REST_Response(713$response->get_data(),714$response->get_status(),715$response->get_headers()716);717}718719returnnewWP_REST_Response($response);720}
History
Introduced in 4.4.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/rest-api.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.