rest_ensure_response( WP_REST_Response|WP_Error|WP_HTTP_Response|mixed $response )
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.
Parameters
- $response
-
(WP_REST_Response|WP_Error|WP_HTTP_Response|mixed) (Required) Response to check.
Return
(WP_REST_Response|WP_Error) If response generated an error, WP_Error, if response is already an instance, WP_REST_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_REST_Response ) { return $response; } // While WP_HTTP_Response is the base class of WP_REST_Response, it doesn't provide // all the required methods used in WP_REST_Server::dispatch(). if ( $response instanceof WP_HTTP_Response ) { return new WP_REST_Response( $response->get_data(), $response->get_status(), $response->get_headers() ); } return new WP_REST_Response( $response ); }
Related
Uses
Uses | Description |
---|---|
wp-includes/load.php: is_wp_error() | Checks whether the given variable is a WordPress Error. |
Used By
Used By | Description |
---|---|
wp-includes/rest-api/endpoints/class-wp-rest-widgets-controller.php: WP_REST_Widgets_Controller::prepare_item_for_response() | Prepares the widget for the REST response. |
wp-includes/rest-api/endpoints/class-wp-rest-sidebars-controller.php: WP_REST_Sidebars_Controller::get_items() | Retrieves the list of sidebars (active or inactive). |
wp-includes/rest-api/endpoints/class-wp-rest-sidebars-controller.php: WP_REST_Sidebars_Controller::prepare_item_for_response() | Prepares a single sidebar output for response. |
wp-includes/rest-api/endpoints/class-wp-rest-templates-controller.php: WP_REST_Templates_Controller::prepare_item_for_response() | Prepare a single template output for response |
wp-includes/rest-api/endpoints/class-wp-rest-templates-controller.php: WP_REST_Templates_Controller::get_items() | Returns a list of templates. |
wp-includes/rest-api/endpoints/class-wp-rest-widget-types-controller.php: WP_REST_Widget_Types_Controller::get_item() | Retrieves a single widget type from the collection. |
wp-includes/rest-api/endpoints/class-wp-rest-widget-types-controller.php: WP_REST_Widget_Types_Controller::prepare_item_for_response() | Prepares a widget type object for serialization. |
wp-includes/rest-api/endpoints/class-wp-rest-widget-types-controller.php: WP_REST_Widget_Types_Controller::encode_form_data() | An RPC-style endpoint which can be used by clients to turn user input in a widget admin form into an encoded instance object. |
wp-includes/rest-api/endpoints/class-wp-rest-widget-types-controller.php: WP_REST_Widget_Types_Controller::get_items() | Retrieves the list of all widget types. |
wp-includes/rest-api/endpoints/class-wp-rest-themes-controller.php: WP_REST_Themes_Controller::get_item() | Retrieves a single theme. |
wp-includes/rest-api/class-wp-rest-server.php: WP_REST_Server::serve_batch_request_v1() | Serves the batch/v1 request. |
wp-includes/rest-api/class-wp-rest-server.php: WP_REST_Server::respond_to_request() | Dispatches the request to the callback handler. |
wp-includes/rest-api/endpoints/class-wp-rest-block-directory-controller.php: WP_REST_Block_Directory_Controller::get_items() | Search and retrieve blocks metadata |
wp-includes/rest-api/endpoints/class-wp-rest-block-types-controller.php: WP_REST_Block_Types_Controller::get_item() | Retrieves a specific block type. |
wp-includes/rest-api/endpoints/class-wp-rest-block-types-controller.php: WP_REST_Block_Types_Controller::prepare_item_for_response() | Prepares a block type object for serialization. |
wp-includes/rest-api/endpoints/class-wp-rest-block-types-controller.php: WP_REST_Block_Types_Controller::get_items() | Retrieves all post block types, depending on user context. |
wp-includes/rest-api/endpoints/class-wp-rest-search-controller.php: WP_REST_Search_Controller::get_items() | Retrieves a collection of search results. |
wp-includes/rest-api/endpoints/class-wp-rest-search-controller.php: WP_REST_Search_Controller::prepare_item_for_response() | Prepares a single search result for response. |
wp-includes/rest-api/endpoints/class-wp-rest-themes-controller.php: WP_REST_Themes_Controller::get_items() | Retrieves a collection of themes. |
wp-includes/rest-api/endpoints/class-wp-rest-themes-controller.php: WP_REST_Themes_Controller::prepare_item_for_response() | Prepares a single theme output for response. |
wp-includes/rest-api/endpoints/class-wp-rest-autosaves-controller.php: WP_REST_Autosaves_Controller::create_item() | Creates, updates or deletes an autosave revision. |
wp-includes/rest-api/endpoints/class-wp-rest-autosaves-controller.php: WP_REST_Autosaves_Controller::get_items() | Gets a collection of autosaves using wp_get_post_autosave. |
wp-includes/rest-api/endpoints/class-wp-rest-block-renderer-controller.php: WP_REST_Block_Renderer_Controller::get_item() | Returns block output from block’s registered render_callback. |
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-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::get_items() | Retrieves all users. |
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-revisions-controller.php: WP_REST_Revisions_Controller::get_items() | Gets a collection of revisions. |
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-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-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-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::get_items() | Retrieves terms associated with a taxonomy. |
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-posts-controller.php: WP_REST_Posts_Controller::create_item() | Creates a single post. |
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_item() | Retrieves 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-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-taxonomies-controller.php: WP_REST_Taxonomies_Controller::get_items() | Retrieves all public taxonomies. |
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-comments-controller.php: WP_REST_Comments_Controller::prepare_item_for_response() | Prepares a single comment output for response. |
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::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::get_items() | Retrieves a list of comment items. |
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/class-wp-rest-server.php: WP_REST_Server::envelope_response() | Wraps the response in an envelope. |
wp-includes/rest-api/class-wp-rest-server.php: WP_REST_Server::serve_request() | Handles serving a REST 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. |
Changelog
Version | Description |
---|---|
4.4.0 | Introduced. |
© 2003–2021 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/functions/rest_ensure_response