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.
659functionrest_ensure_response($response){660if(is_wp_error($response)){661return$response;662}663664if($responseinstanceofWP_REST_Response){665return$response;666}667668/*669 * While WP_HTTP_Response is the base class of WP_REST_Response, it doesn't provide670 * all the required methods used in WP_REST_Server::dispatch().671 */672if($responseinstanceofWP_HTTP_Response){673returnnewWP_REST_Response(674$response->get_data(),675$response->get_status(),676$response->get_headers()677);678}679680returnnewWP_REST_Response($response);681}
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 6.7.7 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.