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.
686functionrest_ensure_response($response){687if(is_wp_error($response)){688return$response;689}690691if($responseinstanceofWP_REST_Response){692return$response;693}694695/*696 * While WP_HTTP_Response is the base class of WP_REST_Response, it doesn't provide697 * all the required methods used in WP_REST_Server::dispatch().698 */699if($responseinstanceofWP_HTTP_Response){700returnnewWP_REST_Response(701$response->get_data(),702$response->get_status(),703$response->get_headers()704);705}706707returnnewWP_REST_Response($response);708}
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.9.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.