WP_REST_Menu_Locations_Controller::prepare_item_for_response() WordPress Method

The WP_REST_Menu_Locations_Controller::prepare_item_for_response() method is used to prepare a menu item for a response. This is used to add the menu item to the appropriate menu location.

WP_REST_Menu_Locations_Controller::prepare_item_for_response( stdClass $item, WP_REST_Request $request ) #

Prepares a menu location object for serialization.


Parameters

$item

(stdClass)(Required)Post status data.

$request

(WP_REST_Request)(Required)Full details about the request.


Top ↑

Return

(WP_REST_Response) Menu location data.


Top ↑

Source

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

	public function prepare_item_for_response( $item, $request ) {
		// Restores the more descriptive, specific name for use within this method.
		$location  = $item;
		$locations = get_nav_menu_locations();
		$menu      = isset( $locations[ $location->name ] ) ? $locations[ $location->name ] : 0;

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

		if ( rest_is_field_included( 'name', $fields ) ) {
			$data['name'] = $location->name;
		}

		if ( rest_is_field_included( 'description', $fields ) ) {
			$data['description'] = $location->description;
		}

		if ( rest_is_field_included( 'menu', $fields ) ) {
			$data['menu'] = (int) $menu;
		}

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

		$response = rest_ensure_response( $data );

		$response->add_links( $this->prepare_links( $location ) );

		/**
		 * Filters menu location data returned from the REST API.
		 *
		 * @since 5.9.0
		 *
		 * @param WP_REST_Response $response The response object.
		 * @param object           $location The original location object.
		 * @param WP_REST_Request  $request  Request used to generate the response.
		 */
		return apply_filters( 'rest_prepare_menu_location', $response, $location, $request );
	}


Top ↑

Changelog

Changelog
VersionDescription
5.9.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.