W3cubDocs

/WordPress

wp_is_serving_rest_request(): bool

Determines whether WordPress is currently serving a REST API request.

Description

The function relies on the ‘REST_REQUEST’ global. As such, it only returns true when an actual REST request is being made. It does not return true when a REST endpoint is hit as part of another request, e.g. for preloading a REST response. See wp_is_rest_endpoint() for that purpose.

This function should not be called until the ‘parse_request’ action, as the constant is only defined then, even for an actual REST request.

Return

bool True if it’s a WordPress REST API request, false otherwise.

Source

function wp_is_serving_rest_request() {
	return defined( 'REST_REQUEST' ) && REST_REQUEST;
}

Changelog

Version Description
6.5.0 Introduced.

© 2003–2024 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/functions/wp_is_serving_rest_request