Skip to content

Instantly share code, notes, and snippets.

@cyberwani
Forked from matzeeable/is_rest.php
Created March 30, 2023 17:38
Show Gist options
  • Save cyberwani/b1ad118f6bbf763af710523af4bbdf7b to your computer and use it in GitHub Desktop.
Save cyberwani/b1ad118f6bbf763af710523af4bbdf7b to your computer and use it in GitHub Desktop.
Checks if the current request is a WP REST API request.
if (!function_exists('is_rest')) {
/**
* Checks if the current request is a WP REST API request.
*
* Case #1: After WP_REST_Request initialisation
* Case #2: Support "plain" permalink settings
* Case #3: URL Path begins with wp-json/ (your REST prefix)
*
* @returns boolean
* @author matzeeable
*/
function is_rest() {
$prefix = rest_get_url_prefix( );
if (defined('REST_REQUEST') && REST_REQUEST // (#1)
|| isset($_GET['rest_route']) // (#2)
&& strpos( trim( $_GET['rest_route'], '\\/' ), $prefix , 0 ) === 0)
return true;
// (#3)
$rest_url = wp_parse_url( site_url( $prefix ) );
$current_url = wp_parse_url( home_url( add_query_arg( array( ) ) ) );
return strpos( $current_url['path'], $rest_url['path'], 0 ) === 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment