Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    Untested, but try hooking the ‘rest_pre_dispatch’ filter. We will be using this hook as an action to send headers. The hook is meant to be a filter where plugins can form their own REST response instead of relying on the default API methods. We don’t want to do that, so have your callback return null so that the API methods are still used.

    This hook fires after the WP_REST_Server object sends its headers, but before any API callbacks are executed, so it should still be possible to send headers. The WP_REST_Server instance is passed to your callback as the second parameter, so you can use that instance’s send_header() method to send any other headers your response requires.

    A little background info: The normal response headers are sent from WP_REST_Server::serve_request(). Oddly, there are no actions allowing us to send our own headers at this point. This method does some administrative things, then calls WP_REST_Server::dispatch() to output the response by calling all the appropriate API callbacks. Just before dispatch() makes these calls, it fires the ‘rest_pre_dispatch’ filter that we use as an action.

    Thread Starter tomfadial

    (@tomfadial)

    Thanks for the help @bcworkz, I’ll take a closer look!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘REST API headers’ is closed to new replies.