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.


Top ↑

Return

(WP_REST_Response|WP_Error) Response object on success, or WP_Error object on failure.


Top ↑

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 )
		);
	}


Top ↑

Changelog

Changelog
VersionDescription
4.7.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.