1

In my themes functions.php I call get_rest_url() and I get the url http://mywebsite.com/wp-json/. But thats not the full url. I need something like http://mywebsite.com/wp-json/wp/v2/

Is there a WordPress function that will give me this url?

1 Answer 1

4

You could add the path argument to the get_rest_url() function as per the WordPress Developer docs - https://developer.wordpress.org/reference/functions/get_rest_url/ - in order to get the path you need:

get_rest_url( null, 'wp/v2/' );

This would produce: http://mywebsite.com/wp-json/wp/v2/

5
  • thanks, will all versions of WordPress have/handle wp/v2? I don't want to hardcode this. This is why I am using get_rest_url() otherwise I would just hard code /wp-json/wp/v2 and not use the function at all.
    – sazr
    Apr 15, 2018 at 12:50
  • As far as I know this is the only way to get that path, as per the Codex - developer.wordpress.org/reference/functions/get_rest_url - I would imagine that if there's ever a v3 then the v2 would remain in place.
    – Mat
    Apr 15, 2018 at 12:54
  • Please edit your answer, and add an explanation: why could that solve the problem?
    – fuxia
    Apr 15, 2018 at 13:07
  • 1
    @JakeM, I don't know about the wp part, but as the name implies it is very possible that there will be a v3 and you will have to hardcode as different versions will have different format of inputs and outputs Apr 15, 2018 at 16:03
  • Could you please accept my answer if it helped?
    – Mat
    Apr 16, 2018 at 17:49

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.