WP_REST_Menu_Items_Controller::prepare_item_for_response( WP_Post $item, WP_REST_Request $request ): WP_REST_Response
- Since
- 5.9.0
- Source
wp-includes/rest-api/endpoints/class-wp-rest-menu-items-controller.php:508
Prepares a single nav menu item output for response.
Parameters
$itemWP_Post- Post object.
$requestWP_REST_Request- Request object.
Return
WP_REST_Response- Response object.
Hooks fired · 2
2 hooks fire while WP_REST_Menu_Items_Controller::prepare_item_for_response() runs, in this order:
- apply_filters( rest_prepare_nav_menu_item )filterline 654 (+146 into the body)
Filters the menu item data for a REST API response.
Uses · 16
- rest_is_field_included()Given an array of fields to include in a response, some of which may be `nested.fields`, determine whether the provided field should be included in the response body.
- add_filter()Adds a callback function to a filter hook.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- remove_filter()Removes a callback function from a filter hook.
- absint()Converts a value to non-negative integer.
- wp_list_filter()Filters a list of objects, based on a set of key => value arguments.
- get_object_taxonomies()Returns the names or objects of the taxonomies which are registered for the requested object or object type, such as a post object or post type name.
- get_the_terms()Retrieves the terms of the taxonomy that are attached to the post.
- wp_list_pluck()Plucks a certain field out of each object or array in an array.
- rest_ensure_response()Ensures a REST response is a response object (for consistency).
- WP_REST_Menu_Items_Controller::get_fields_for_response()
- WP_REST_Menu_Items_Controller::get_nav_menu_item()Gets the nav menu item, if the ID is valid.
Show all 16
- WP_REST_Menu_Items_Controller::add_additional_fields_to_object()
- WP_REST_Menu_Items_Controller::filter_response_by_context()
- WP_REST_Menu_Items_Controller::prepare_links()Prepares links for the request.
- WP_REST_Menu_Items_Controller::get_available_actions()
Used by · 3
- WP_REST_Menu_Items_Controller::create_item()Creates a single nav menu item.
- WP_REST_Menu_Items_Controller::delete_item()Deletes a single nav menu item.
- WP_REST_Menu_Items_Controller::update_item()Updates a single nav menu item.
Source
public function prepare_item_for_response( $item, $request ) { // Base fields for every post. $fields = $this->get_fields_for_response( $request ); $menu_item = $this->get_nav_menu_item( $item->ID ); $data = array(); if ( rest_is_field_included( 'id', $fields ) ) { $data['id'] = $menu_item->ID; } if ( rest_is_field_included( 'title', $fields ) ) { $data['title'] = array(); } if ( rest_is_field_included( 'title.raw', $fields ) ) { $data['title']['raw'] = $menu_item->title; } if ( rest_is_field_included( 'title.rendered', $fields ) ) { add_filter( 'protected_title_format', array( $this, 'protected_title_format' ) ); add_filter( 'private_title_format', array( $this, 'protected_title_format' ) ); /** This filter is documented in wp-includes/post-template.php */ $title = apply_filters( 'the_title', $menu_item->title, $menu_item->ID ); $data['title']['rendered'] = $title; remove_filter( 'protected_title_format', array( $this, 'protected_title_format' ) ); remove_filter( 'private_title_format', array( $this, 'protected_title_format' ) ); } if ( rest_is_field_included( 'status', $fields ) ) { $data['status'] = $menu_item->post_status; } if ( rest_is_field_included( 'url', $fields ) ) { $data['url'] = $menu_item->url; } if ( rest_is_field_included( 'attr_title', $fields ) ) { // Same as post_excerpt. $data['attr_title'] = $menu_item->attr_title; } if ( rest_is_field_included( 'description', $fields ) ) { // Same as post_content. $data['description'] = $menu_item->description; } if ( rest_is_field_included( 'type', $fields ) ) { $data['type'] = $menu_item->type; } if ( rest_is_field_included( 'type_label', $fields ) ) { $data['type_label'] = $menu_item->type_label; } if ( rest_is_field_included( 'object', $fields ) ) { $data['object'] = $menu_item->object; } if ( rest_is_field_included( 'object_id', $fields ) ) { // It is stored as a string, but should be exposed as an integer. $data['object_id'] = absint( $menu_item->object_id ); } if ( rest_is_field_included( 'parent', $fields ) ) { // Same as post_parent, exposed as an integer. $data['parent'] = (int) $menu_item->menu_item_parent; } if ( rest_is_field_included( 'menu_order', $fields ) ) { // Same as post_parent, exposed as an integer. $data['menu_order'] = (int) $menu_item->menu_order; } if ( rest_is_field_included( 'target', $fields ) ) { $data['target'] = $menu_item->target; }History
Introduced in 5.9.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 6.9.7 tag, from
src/wp-includes/rest-api/endpoints/class-wp-rest-menu-items-controller.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.