I’m currently looking for a way to add additional data to a WordPress REST request directly when it reaches WordPress so that I can access the data "later" in each REST route callback.
I’ve tried to set it in the rest_pre_dispatch filter but it seems to be not correct:
add_filter( 'rest_pre_dispatch', [ $this, 'filter_rest_pre_dispatch' ], 10, 3 );
public function filter_rest_pre_dispatch( $result, WP_REST_Server $server, WP_REST_Request $request ) {
$request->set_attributes( [
'test' => '1'
] );
return $result;
}
Has anyone done this before and might help me out?
2
Answers
I've still found no solution for this. Instead I've created an abstract class which extends every class to register an endpoint and its function. In this abstract class I'm doing this for example to get a consumer key:
Thats the only solution in my case.
You could try filter such as
rest_request_after_callbacks
orrest_request_before_callbacks
and methods of the request object like$request->get_attributes()
and$request->set_attributes()
to change the REST endpoint argumentsAlso
$request->set_body_params()
would allow you to manipulate and add additional data to the request