I am attempting to retrieve a response within my custom plugin, as based on the REST routes provided by another plugin.
When running the following block of code in the root scope of my theme's function file I get a successful response with an expected outcome.
$request = new WP_REST_Request('GET', '/another/plugins/rest/route');
$request->set_query_params([
'filterBy' => [
'url' => '/test/'
]
]);
$response = rest_do_request($request);
But when running the exact block of code above in the root scope of my custom plugin file, I get a response of the following:
Array
(
[code] => rest_no_route
[message] => No route was found matching the URL and request method
[data] => Array
(
[status] => 404
)
)
I will note that my custom plugin should load after the other plugin that generated the REST routes, because my custom plugin's name comes alphabetically after the other plugin, and I have no code in place to change that order.
How do I get the rest routes to instantiate before my plugin is loaded?
I am attempting to retrieve a response within my custom plugin, as based on the REST routes provided by another plugin.
When running the following block of code in the root scope of my theme's function file I get a successful response with an expected outcome.
$request = new WP_REST_Request('GET', '/another/plugins/rest/route');
$request->set_query_params([
'filterBy' => [
'url' => '/test/'
]
]);
$response = rest_do_request($request);
But when running the exact block of code above in the root scope of my custom plugin file, I get a response of the following:
Array
(
[code] => rest_no_route
[message] => No route was found matching the URL and request method
[data] => Array
(
[status] => 404
)
)
I will note that my custom plugin should load after the other plugin that generated the REST routes, because my custom plugin's name comes alphabetically after the other plugin, and I have no code in place to change that order.
How do I get the rest routes to instantiate before my plugin is loaded?
Share Improve this question edited May 25, 2020 at 22:41 Mike Kormendy asked May 25, 2020 at 20:02 Mike KormendyMike Kormendy 1971 silver badge10 bronze badges 6 | Show 1 more comment1 Answer
Reset to default 1The fundamental problem is that the code tries to use the REST API endpoint before the endpoints have been registered.
Specifically it needs to do this after the rest_api_init
hook has fired. Try to do it on a hook that runs after plugins and theme are loaded such as wp_loaded
.
In general avoid doing work in the root scope of a plugins file or functions.php
. WordPress is architected around hooks and filters and a particular load process, so it's more accurate to think of hooks as events on a timeline. By just putting stuff in the plugin file without using a hook, it's essentially running in prehistoric times. You can't ask it to drive from A to B because cars haven't been invented yet.
So this isn't a problem of what, it's a problem of when
add_action
/add_filter
– Tom J Nowell ♦ Commented May 26, 2020 at 9:02