0

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?

6
  • It is also good to note that I realize that a theme's function file loads much later in the order of processing, but I would like to get my custom plugin's code to load when the rest route becomes available, I'm not sure how I would do that. Commented May 25, 2020 at 20:05
  • When you say it runs in your plugin, when does it run? Is it on a hook? Do you call it in a function? Is it just in the top level of the file? How is it called? Keep in mind that plugin probably has a better way to access that information, but you'd need to refer to that plugins docs to know
    – Tom J Nowell
    Commented May 25, 2020 at 20:47
  • @TomJNowell I updated my post - it's in the root scope of the plugin file. Not in a function, hook or otherwise. Commented May 25, 2020 at 22:42
  • 1
    Ah that explains it then, avoid doing work in the root scope of plugin files, everything should be on a hook, unless it's creating objects or calling add_action/add_filter
    – Tom J Nowell
    Commented May 26, 2020 at 9:02
  • I can do that, in fact it will be destined for being called within a few action hooks anyhow, but I was doing some extensive CRUD scenario testing and thought it odd that the rest routes weren't available in my scenario. Thanks! Commented May 26, 2020 at 23:00

1 Answer 1

1

The 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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