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
- I would like to save data depending on parameters.
- I want to add some business logic
- How about this, rest_pre_dispatch – Buttered_Toast Commented Jun 10, 2021 at 7:43
- 1 It's not clear what you are attempting to accomplish. Are you looking for a hook which fires when the WordPress REST API controllers are handling any request? – bosco Commented Jun 11, 2021 at 0:34
- Hi @bosco , Sure, I need hook for any incoming request from api? So, I could add business logic depending on what parameters included – Por Commented Jun 11, 2021 at 2:03
1 Answer
Reset to default 0There'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.