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
}
Is the WordPress REST API enabled? The best way to check is to visit this URL: https://yoursite.com/wp-json. If you see some information which seems related to your WordPress REST API, it works. If you see something, it means that, at least, your WordPress REST API is enabled.
The REST API is a developer-oriented feature of WordPress.
Get an API key for the API. Create a Plugin for adding a widget to your WordPress site. Customize your plugin with your API key and specific information you want to display in the widget. Use the WordPress Admin Area to place the widget on your site where you want it within your theme.
In particular, the WordPress REST API enables you to connect your WordPress website with external applications. This means you can develop a mobile app using practically any programming language, and use the WP REST API to fetch data from WordPress.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With