rest_ensure_response() WordPress Function
The rest_ensure_response() function is used to make sure that a WordPress REST API response is a valid response object. This function can be used to wrap a WordPress REST API response in an array, or it can be used to wrap a WordPress REST API response in an object.
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 ); }
Expand full source codeCollapse full source codeView on TracView on GitHub
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-block-patterns-controller.php:WP_REST_Block_Patterns_Controller::get_items() | Retrieves all block patterns. |
wp-includes/rest-api/endpoints/class-wp-rest-block-patterns-controller.php:WP_REST_Block_Patterns_Controller::prepare_item_for_response() | Prepare a raw block pattern before it gets output in a REST API response. |
wp-includes/rest-api/endpoints/class-wp-rest-global-styles-controller.php:WP_REST_Global_Styles_Controller::get_theme_items() | Returns the given theme global styles variations. |
wp-includes/rest-api/endpoints/class-wp-rest-block-pattern-categories-controller.php:WP_REST_Block_Pattern_Categories_Controller::get_items() | Retrieves all block pattern categories. |
wp-includes/rest-api/endpoints/class-wp-rest-block-pattern-categories-controller.php:WP_REST_Block_Pattern_Categories_Controller::prepare_item_for_response() | Prepare a raw block pattern category before it gets output in a REST API response. |
wp-includes/rest-api/endpoints/class-wp-rest-menu-items-controller.php:WP_REST_Menu_Items_Controller::prepare_item_for_response() | Prepares a single post output for response. |
wp-includes/rest-api/endpoints/class-wp-rest-menu-items-controller.php:WP_REST_Menu_Items_Controller::create_item() | Creates a single post. |
wp-includes/rest-api/endpoints/class-wp-rest-menu-items-controller.php:WP_REST_Menu_Items_Controller::update_item() | Updates a single nav menu item. |
wp-includes/rest-api/endpoints/class-wp-rest-global-styles-controller.php:WP_REST_Global_Styles_Controller::get_theme_item() | Returns the given theme global styles config. |
wp-includes/rest-api/endpoints/class-wp-rest-global-styles-controller.php:WP_REST_Global_Styles_Controller::update_item() | Updates a single global style config. |
wp-includes/rest-api/endpoints/class-wp-rest-global-styles-controller.php:WP_REST_Global_Styles_Controller::prepare_item_for_response() | Prepare a global styles config output for response. |
wp-includes/rest-api/endpoints/class-wp-rest-url-details-controller.php:WP_REST_URL_Details_Controller::parse_url_details() | Retrieves the contents of the title tag from the HTML response. |
wp-includes/rest-api/endpoints/class-wp-rest-menus-controller.php:WP_REST_Menus_Controller::prepare_item_for_response() | Prepares a single term output for response. |
wp-includes/rest-api/endpoints/class-wp-rest-menus-controller.php:WP_REST_Menus_Controller::create_item() | Creates a single term in a taxonomy. |
wp-includes/rest-api/endpoints/class-wp-rest-menus-controller.php:WP_REST_Menus_Controller::update_item() | Updates a single term from a taxonomy. |
wp-includes/rest-api/endpoints/class-wp-rest-menu-locations-controller.php:WP_REST_Menu_Locations_Controller::get_items() | Retrieves all menu locations, depending on user context. |
wp-includes/rest-api/endpoints/class-wp-rest-menu-locations-controller.php:WP_REST_Menu_Locations_Controller::get_item() | Retrieves a specific menu location. |
wp-includes/rest-api/endpoints/class-wp-rest-menu-locations-controller.php:WP_REST_Menu_Locations_Controller::prepare_item_for_response() | Prepares a menu location object for serialization. |
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-templates-controller.php:WP_REST_Templates_Controller::update_item() | Updates a single template. |
wp-includes/rest-api/endpoints/class-wp-rest-templates-controller.php:WP_REST_Templates_Controller::create_item() | Creates a single template. |
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.php:rest_preload_api_request() | Append result of internal request to REST API for purpose of preloading data to be attached to a page. |
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. |