The rest_ensure_request() WordPress PHP function ensures request arguments are a WP_REST_Request object (for consistency).
Usage
$result = rest_ensure_request( $request );
Parameters
$request
(array|string|WP_REST_Request) (Required) – The request to check and ensure it is a WP_REST_Request object.
More information
See WordPress Developer Resources: rest_ensure_request()
Examples
Ensuring a request from an array
// Create a request array $request_array = array( 'method' => 'GET', 'path' => '/wp/v2/posts' ); // Ensure the request is a WP_REST_Request object $request_object = rest_ensure_request( $request_array );
Ensuring a request from a string
// Create a request string $request_string = '/wp/v2/posts'; // Ensure the request is a WP_REST_Request object $request_object = rest_ensure_request( $request_string );
Ensuring a request is a WP_REST_Request object
// Create a WP_REST_Request object $request_object = new WP_REST_Request( 'GET', '/wp/v2/posts' ); // Ensure the request is a WP_REST_Request object $ensured_request = rest_ensure_request( $request_object );
Ensuring a request with query parameters
// Create a request array with query parameters $request_array = array( 'method' => 'GET', 'path' => '/wp/v2/posts', 'params' => array( 'per_page' => 5 ) ); // Ensure the request is a WP_REST_Request object $request_object = rest_ensure_request( $request_array );
Ensuring a request with custom headers
// Create a request array with custom headers $request_array = array( 'method' => 'GET', 'path' => '/wp/v2/posts', 'headers' => array( 'X-WP-Nonce' => '123456' ) ); // Ensure the request is a WP_REST_Request object $request_object = rest_ensure_request( $request_array );