WP_REST_Menu_Items_Controller::create_item()publicWP 5.9.0

Creates a single post.

{} It's a method of the class: WP_REST_Menu_Items_Controller{}

Return

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

Usage

$WP_REST_Menu_Items_Controller = new WP_REST_Menu_Items_Controller();
$WP_REST_Menu_Items_Controller->create_item( $request );
$request(WP_REST_Request) (required)
Full details about the request.

Changelog

Since 5.9.0 Introduced.

Code of WP_REST_Menu_Items_Controller::create_item() WP 6.0

public function create_item( $request ) {
	if ( ! empty( $request['id'] ) ) {
		return new WP_Error( 'rest_post_exists', __( 'Cannot create existing post.' ), array( 'status' => 400 ) );
	}

	$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_insert_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;
	}

	/**
	 * Fires after a single menu item is created or updated via the REST API.
	 *
	 * @since 5.9.0
	 *
	 * @param object          $nav_menu_item Inserted or updated menu item object.
	 * @param WP_REST_Request $request       Request object.
	 * @param bool            $creating      True when creating a menu item, false when updating.
	 */
	do_action( 'rest_insert_nav_menu_item', $nav_menu_item, $request, true );

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

	$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' );

	/**
	 * Fires after a single menu item is completely created or updated via the REST API.
	 *
	 * @since 5.9.0
	 *
	 * @param object          $nav_menu_item Inserted or updated menu item object.
	 * @param WP_REST_Request $request       Request object.
	 * @param bool            $creating      True when creating a menu item, false when updating.
	 */
	do_action( 'rest_after_insert_nav_menu_item', $nav_menu_item, $request, true );

	$post = get_post( $nav_menu_item_id );
	wp_after_insert_post( $post, false, null );

	$response = $this->prepare_item_for_response( $post, $request );
	$response = rest_ensure_response( $response );

	$response->set_status( 201 );
	$response->header( 'Location', rest_url( sprintf( '%s/%s/%d', $this->namespace, $this->rest_base, $nav_menu_item_id ) ) );

	return $response;
}