最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

trigger WordPress rest any API call

programmeradmin1浏览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

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
Share Improve this question edited Jun 11, 2021 at 10:25 Rup 4,3904 gold badges28 silver badges29 bronze badges asked Jun 10, 2021 at 5:50 PorPor 3823 silver badges12 bronze badges 3
  • 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
Add a comment  | 

1 Answer 1

Reset to default 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.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论