Is there any way to hook all incoming requests for the REST API in WordPress? The reason why I want to trigger all API call...is
When API was called
- I would like to save data depending on parameters.
- I want to add some business logic
Is there any way to hook all incoming requests for the REST API in WordPress? The reason why I want to trigger all API call...is
When API was called
There's actually a hook that will fire for all REST API requests. It's the recommended hook to use when adding your own API endpoints, so you can be sure that this hook will be fired for every single REST API request that your website handles.
/**
* @param \WP_REST_Server $wp_rest_server
*/
function capture_all_rest_api_requests( $wp_rest_server ) {
// Your code here to do your custom REST API handling.
}
add_action( 'rest_api_init', 'capture_all_rest_api_requests' );
Unfortunately you haven't provided further details as to what exactly you want to do or achieve with this hook, so we can't be sure this is the most appropriate hook to use.
But you asked for a hook for all incoming requests for the REST API, and this is it.
When this hook is fired, you'll know it's a REST API request and nothing else.