12

What is the best way to check whether a request is an API request?

Note that the request might be a custom API request, which means it may be as follows:

mysite.com/wp-json/my_namespace/my_version/my_action

Obviously, I can check whether the API route wp-json, but there should be a built-in function to check that.

I need it to do some hooks such as

add_action('init', 'do_something_only_if_api_request');

function do_something_only_if_api_request()
{
   if ( ! wp_api_request() ) {
     return;
   }
   // do stuff
}

3 Answers 3

18

You can check defined('REST_REQUEST'). This constant is defined as true in rest_api_loaded() (and not defined otherwise).

There was a discussion of WP Rest API developers about the introduction of a new function like is_rest_request(). In the end they went for this constant.

2
  • 2
    Note that REST_REQUEST is only defined during the "wp" action (see codex.wordpress.org/Plugin_API/Action_Reference), which is much later than the "init" action. Jul 19, 2021 at 19:16
  • Not sure if it's changed since this last comment but I was trying to find where REST_REQUEST was first defined and it looks like it's now in the parse_request action
    – BIOSTALL
    Jan 18 at 10:10
2

As of December 2016, the REST API documentation is pretty poor regarding everything which doesn't look like an endpoint.

However, a few functions exist and you can find the documentation right in the file as they're very well documented, see: wordpress/wp-includes/rest-api.php

If you want add an action only on an REST API call then you probably want to hook the action: rest_api_init, it would look like:

add_action('rest_api_init', 'do_something_only_if_api_request');

function do_something_only_if_api_request($wp_rest_server)
{

   // do stuff

}

You can find the details in the PHPdoc comment:

    /**
     * Fires when preparing to serve an API request.
     *
     * Endpoint objects should be created and register their hooks on this action rather
     * than another action to ensure they're only loaded when needed.
     *
     * @since 4.4.0
     *
     * @param WP_REST_Server $wp_rest_server Server object.
     */
1

In my case, the plugin is doing wp_redirect for users to login page.

I want to avoid this in case that the rest api is called.

So I'm using

if (! strpos( $_SERVER['REQUEST_URI'], 'wp-json')) // It's not a rest-api call
3
  • What if a page/post URL literally has wp-json in it?
    – mukto90
    Feb 14, 2023 at 5:38
  • I would check for REST_REQUEST first, but in case you need to check before that gets defined, perhaps something like if (strpos( $_SERVER['REQUEST_URI'], 'wp-json') == 1) // It is most-likely a rest request
    – Paul
    May 31, 2023 at 23:00
  • This is better approach: if (strpos( $_SERVER['REQUEST_URI'], '/wp-json/') !== 0). The suggested answer will be passed if URL has wp-json inside it e.g. example.com/all-about-wp-json/
    – doraemon
    Nov 19, 2023 at 15:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.