rest_ensure_request() WordPress Function

rest_ensure_request() is a function used to ensure that a request made to the WordPress REST API is valid. This function will check the request method, headers, and body for validity and return an error if any of these are invalid. This function is useful for ensuring that a request made to the WordPress REST API is valid and can be processed by the API.

rest_ensure_request( array|string|WP_REST_Request $request ) #

Ensures request arguments are a request object (for consistency).


Parameters

$request

(array|string|WP_REST_Request)(Required)Request to check.


Top ↑

Return

(WP_REST_Request) REST request instance.


Top ↑

Source

File: wp-includes/rest-api.php

function rest_ensure_request( $request ) {
	if ( $request instanceof WP_REST_Request ) {
		return $request;
	}

	if ( is_string( $request ) ) {
		return new WP_REST_Request( 'GET', $request );
	}

	return new WP_REST_Request( 'GET', '', $request );
}


Top ↑

Changelog

Changelog
VersionDescription
5.3.0Accept string argument for the request path.
4.4.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.

Show More