WP_REST_Controller::prepare_item_for_response() WordPress Method
The WP_REST_Controller::prepare_item_for_response() method is used to prepare a single item for response. This method is called by the WP_REST_Controller::get_item() and WP_REST_Controller::get_items() methods, and should not be called directly. The prepare_item_for_response() method allows for data manipulation and filtering before the item is returned as part of the API response. For example, you may want to remove certain fields from the response, or massage the data in some way before it is returned. The prepare_item_for_response() method takes two parameters: the $request object, and the $item object (which is the data that will be returned in the API response). The prepare_item_for_response() method should return a new $response object, which will be used as the API response.
WP_REST_Controller::prepare_item_for_response( mixed $item, WP_REST_Request $request ) #
Prepares the item for the REST response.
Parameters
- $item
(mixed)(Required)WordPress representation of the item.
- $request
(WP_REST_Request)(Required)Request object.
Return
(WP_REST_Response|WP_Error) Response object on success, or WP_Error object on failure.
Source
File: wp-includes/rest-api/endpoints/class-wp-rest-controller.php
public function prepare_item_for_response( $item, $request ) { return new WP_Error( 'invalid-method', /* translators: %s: Method name. */ sprintf( __( "Method '%s' not implemented. Must be overridden in subclass." ), __METHOD__ ), array( 'status' => 405 ) ); }
Expand full source codeCollapse full source codeView on TracView on GitHub
Changelog
Version | Description |
---|---|
4.7.0 | Introduced. |