WP_REST_Search_Controller::prepare_item_for_response() WordPress Method

The WP_REST_Search_Controller::prepare_item_for_response() method is used to prepare an item for response. This is typically done before the item is registered with the Wordpress REST API.

WP_REST_Search_Controller::prepare_item_for_response( int|string $item, WP_REST_Request $request ) #

Prepares a single search result for response.


Parameters

$item

(int|string)(Required)ID of the item to prepare.

$request

(WP_REST_Request)(Required)Request object.


Top ↑

Return

(WP_REST_Response) Response object.


Top ↑

Source

File: wp-includes/rest-api/endpoints/class-wp-rest-search-controller.php

	public function prepare_item_for_response( $item, $request ) {
		// Restores the more descriptive, specific name for use within this method.
		$item_id = $item;
		$handler = $this->get_search_handler( $request );
		if ( is_wp_error( $handler ) ) {
			return new WP_REST_Response();
		}

		$fields = $this->get_fields_for_response( $request );

		$data = $handler->prepare_item( $item_id, $fields );
		$data = $this->add_additional_fields_to_object( $data, $request );

		$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
		$data    = $this->filter_response_by_context( $data, $context );

		$response = rest_ensure_response( $data );

		$links               = $handler->prepare_item_links( $item_id );
		$links['collection'] = array(
			'href' => rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ),
		);
		$response->add_links( $links );

		return $response;
	}


Top ↑

Changelog

Changelog
VersionDescription
5.9.0Renamed $id to $item to match parent class for PHP 8 named parameter support.
5.6.0The $id parameter can accept a string.
5.0.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.