WP_REST_Menu_Items_Controller::delete_item()publicWP 5.9.0

Deletes a single menu item.

{} Это метод класса: WP_REST_Menu_Items_Controller{}

Хуки из метода

Возвращает

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

Использование

$WP_REST_Menu_Items_Controller = new WP_REST_Menu_Items_Controller();
$WP_REST_Menu_Items_Controller->delete_item( $request );
$request(WP_REST_Request) (обязательный)
Full details about the request.

Список изменений

С версии 5.9.0 Введена.

Код WP_REST_Menu_Items_Controller::delete_item() WP 6.0.2

public function delete_item( $request ) {
	$menu_item = $this->get_nav_menu_item( $request['id'] );
	if ( is_wp_error( $menu_item ) ) {
		return $menu_item;
	}

	// We don't support trashing for menu items.
	if ( ! $request['force'] ) {
		/* translators: %s: force=true */
		return new WP_Error( 'rest_trash_not_supported', sprintf( __( "Menu items do not support trashing. Set '%s' to delete." ), 'force=true' ), array( 'status' => 501 ) );
	}

	$previous = $this->prepare_item_for_response( get_post( $request['id'] ), $request );

	$result = wp_delete_post( $request['id'], true );

	if ( ! $result ) {
		return new WP_Error( 'rest_cannot_delete', __( 'The post cannot be deleted.' ), array( 'status' => 500 ) );
	}

	$response = new WP_REST_Response();
	$response->set_data(
		array(
			'deleted'  => true,
			'previous' => $previous->get_data(),
		)
	);

	/**
	 * Fires immediately after a single menu item is deleted via the REST API.
	 *
	 * @since 5.9.0
	 *
	 * @param object          $nav_menu_item Inserted or updated menu item object.
	 * @param WP_REST_Response $response The response data.
	 * @param WP_REST_Request $request       Request object.
	 */
	do_action( 'rest_delete_nav_menu_item', $menu_item, $response, $request );

	return $response;
}