• Resolved joho68

    (@joho68)


    At what point during the execution of a WordPress plugin, admin or front-end, are static methods of WP_REST_Server available? I’m trying to extract information about the REST setup (such as endpoints and namespaces) for a given site by using functions in WP_REST_Server but end up with errors in the log file.

    Surely, WP must know about its REST environment at a rather early point of a request since it knows how to determine if it’s a REST request or not?

    I’d rather not make an actual REST request to “myself” for things that the REST API already knows and should be able to give me directly.

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

    (@bcworkz)

    WP loads the API class declarations on any given request, but nothing is instantiated unless it’s for an actual API request. I’m skeptical that you could successfully instantiate the server yourself because much of the API code is intrinsically tied to the API request. Without a proper request, I’m pretty sure it will not work as expected.

    It does seem odd that you’d have to make an external request to get API data, but FWIW core WP already does this all the time. It’s not as odd as it may seem.

    Thread Starter joho68

    (@joho68)

    Thanks for responding. But since WP knows when it’s a REST request and when it’s not, it would seem reasonable to expect that some of this is accessible rather early, no? I mean, it has to know about at least namespaces when checking a request, right?

    What I’m doing now, in my class, is something like:

    
    if ( $this->my_rest_server == null ) {
        $this->my_rest_server = rest_get_server();
    }
    

    and it seems to work like I want/need it to, but I’m not sure it’s the “correct” way of doing it the “WordPress way” ??

    • This reply was modified 4 years ago by joho68.
    • This reply was modified 4 years ago by joho68.
    Moderator bcworkz

    (@bcworkz)

    I’ve seen the same thing in core code FWIW. We might be at a level of detail that’s beyond any accepted “right” way. Where if it doesn’t break anything it’s probably fine.

    Thread Starter joho68

    (@joho68)

    Fair enough. I’ll carry on using this method until I find a better way ?? Thanks.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘When is WP_REST_Server available?’ is closed to new replies.