WP_REST_Menu_Items_Controller::update_item( WP_REST_Request $request ): WP_REST_Response|WP_Error

In this article

Updates a single nav menu item.

Parameters

$requestWP_REST_Requestrequired
Full details about the request.

Return

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

Source

public function update_item( $request ) {
	$valid_check = $this->get_nav_menu_item( $request['id'] );
	if ( is_wp_error( $valid_check ) ) {
		return $valid_check;
	}
	$post_before       = get_post( $request['id'] );
	$prepared_nav_item = $this->prepare_item_for_database( $request );

	if ( is_wp_error( $prepared_nav_item ) ) {
		return $prepared_nav_item;
	}

	$prepared_nav_item = (array) $prepared_nav_item;

	$nav_menu_item_id = wp_update_nav_menu_item( $prepared_nav_item['menu-id'], $prepared_nav_item['menu-item-db-id'], wp_slash( $prepared_nav_item ), false );

	if ( is_wp_error( $nav_menu_item_id ) ) {
		if ( 'db_update_error' === $nav_menu_item_id->get_error_code() ) {
			$nav_menu_item_id->add_data( array( 'status' => 500 ) );
		} else {
			$nav_menu_item_id->add_data( array( 'status' => 400 ) );
		}

		return $nav_menu_item_id;
	}

	$nav_menu_item = $this->get_nav_menu_item( $nav_menu_item_id );
	if ( is_wp_error( $nav_menu_item ) ) {
		$nav_menu_item->add_data( array( 'status' => 404 ) );

		return $nav_menu_item;
	}

	/** This action is documented in wp-includes/rest-api/endpoints/class-wp-rest-menu-items-controller.php */
	do_action( 'rest_insert_nav_menu_item', $nav_menu_item, $request, false );

	$schema = $this->get_item_schema();

	if ( ! empty( $schema['properties']['meta'] ) && isset( $request['meta'] ) ) {
		$meta_update = $this->meta->update_value( $request['meta'], $nav_menu_item->ID );

		if ( is_wp_error( $meta_update ) ) {
			return $meta_update;
		}
	}

	$post          = get_post( $nav_menu_item_id );
	$nav_menu_item = $this->get_nav_menu_item( $nav_menu_item_id );
	$fields_update = $this->update_additional_fields_for_object( $nav_menu_item, $request );

	if ( is_wp_error( $fields_update ) ) {
		return $fields_update;
	}

	$request->set_param( 'context', 'edit' );

	/** This action is documented in wp-includes/rest-api/endpoints/class-wp-rest-menu-items-controller.php */
	do_action( 'rest_after_insert_nav_menu_item', $nav_menu_item, $request, false );

	wp_after_insert_post( $post, true, $post_before );

	$response = $this->prepare_item_for_response( get_post( $nav_menu_item_id ), $request );

	return rest_ensure_response( $response );
}

Hooks

do_action( ‘rest_after_insert_nav_menu_item’, object $nav_menu_item, WP_REST_Request $request, bool $creating )

Fires after a single menu item is completely created or updated via the REST API.

do_action( ‘rest_insert_nav_menu_item’, object $nav_menu_item, WP_REST_Request $request, bool $creating )

Fires after a single menu item is created or updated via the REST API.

Changelog

VersionDescription
5.9.0Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.