rest_ensure_response( WP_Error|WP_HTTP_Response|mixed $response )
Ensures a REST response is a response object (for consistency).
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.
Parameters
- $response
-
(Required) Response to check.
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.
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 );
}
Related
Uses
Uses | Description |
---|---|
wp-includes/load.php: is_wp_error() |
Check whether variable is a ClassicPress Error. |
Used By
Used By | Description |
---|---|
wp-includes/rest-api/class-wp-rest-server.php: WP_REST_Server::dispatch() |
Matches the request to a callback and call it. |
wp-includes/rest-api/class-wp-rest-server.php: WP_REST_Server::get_namespace_index() |
Retrieves the index for a namespace. |
wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php: WP_REST_Comments_Controller::get_items() |
Retrieves a list of comment items. |
wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php: WP_REST_Comments_Controller::get_item() |
Retrieves a comment. |
wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php: WP_REST_Comments_Controller::create_item() |
Creates a comment. |
wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php: WP_REST_Comments_Controller::update_item() |
Updates a comment. |
wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php: WP_REST_Comments_Controller::prepare_item_for_response() |
Prepares a single comment output for response. |
wp-includes/rest-api/class-wp-rest-server.php: WP_REST_Server::serve_request() |
Handles serving an API request. |
wp-includes/rest-api/class-wp-rest-server.php: WP_REST_Server::embed_links() |
Embeds the links from the data into the request. |
wp-includes/rest-api/class-wp-rest-server.php: WP_REST_Server::envelope_response() |
Wraps the response in an envelope. |
wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php: WP_REST_Users_Controller::get_items() |
Retrieves all users. |
wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php: WP_REST_Users_Controller::get_item() |
Retrieves a single user. |
wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php: WP_REST_Users_Controller::get_current_item() |
Retrieves the current user. |
wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php: WP_REST_Users_Controller::create_item() |
Creates a single user. |
wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php: WP_REST_Users_Controller::update_item() |
Updates a single user. |
wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php: WP_REST_Users_Controller::prepare_item_for_response() |
Prepares a single user output for response. |
wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php: WP_REST_Terms_Controller::get_items() |
Retrieves terms associated with a taxonomy. |
wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php: WP_REST_Terms_Controller::get_item() |
Gets a single term from a taxonomy. |
wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php: WP_REST_Terms_Controller::create_item() |
Creates a single term in a taxonomy. |
wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php: WP_REST_Terms_Controller::update_item() |
Updates a single term from a taxonomy. |
wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php: WP_REST_Terms_Controller::prepare_item_for_response() |
Prepares a single term output for response. |
wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php: WP_REST_Attachments_Controller::create_item() |
Creates a single attachment. |
wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php: WP_REST_Attachments_Controller::update_item() |
Updates a single attachment. |
wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php: WP_REST_Attachments_Controller::prepare_item_for_response() |
Prepares a single attachment output for response. |
wp-includes/rest-api/endpoints/class-wp-rest-taxonomies-controller.php: WP_REST_Taxonomies_Controller::get_items() |
Retrieves all public taxonomies. |
wp-includes/rest-api/endpoints/class-wp-rest-taxonomies-controller.php: WP_REST_Taxonomies_Controller::get_item() |
Retrieves a specific taxonomy. |
wp-includes/rest-api/endpoints/class-wp-rest-taxonomies-controller.php: WP_REST_Taxonomies_Controller::prepare_item_for_response() |
Prepares a taxonomy object for serialization. |
wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php: WP_REST_Revisions_Controller::get_items() |
Gets a collection of revisions. |
wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php: WP_REST_Revisions_Controller::get_item() |
Retrieves one revision from the collection. |
wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php: WP_REST_Revisions_Controller::prepare_item_for_response() |
Prepares the revision for the REST response. |
wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php: WP_REST_Posts_Controller::prepare_item_for_response() |
Prepares a single post output for response. |
wp-includes/rest-api/endpoints/class-wp-rest-post-types-controller.php: WP_REST_Post_Types_Controller::get_items() |
Retrieves all public post types. |
wp-includes/rest-api/endpoints/class-wp-rest-post-types-controller.php: WP_REST_Post_Types_Controller::get_item() |
Retrieves a specific post type. |
wp-includes/rest-api/endpoints/class-wp-rest-post-types-controller.php: WP_REST_Post_Types_Controller::prepare_item_for_response() |
Prepares a post type object for serialization. |
wp-includes/rest-api/endpoints/class-wp-rest-post-statuses-controller.php: WP_REST_Post_Statuses_Controller::get_items() |
Retrieves all post statuses, depending on user context. |
wp-includes/rest-api/endpoints/class-wp-rest-post-statuses-controller.php: WP_REST_Post_Statuses_Controller::get_item() |
Retrieves a specific post status. |
wp-includes/rest-api/endpoints/class-wp-rest-post-statuses-controller.php: WP_REST_Post_Statuses_Controller::prepare_item_for_response() |
Prepares a post status object for serialization. |
wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php: WP_REST_Posts_Controller::update_item() |
Updates a single post. |
wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php: WP_REST_Posts_Controller::get_items() |
Retrieves a collection of posts. |
wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php: WP_REST_Posts_Controller::get_item() |
Retrieves a single post. |
wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php: WP_REST_Posts_Controller::create_item() |
Creates a single post. |
Changelog
Version | Description |
---|---|
WP-4.4.0 | Introduced. |