0

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
3
  • How about this, rest_pre_dispatch Jun 10 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
    Jun 11 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
    Jun 11 at 2:03
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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