wordpresswordpress-rest-api

What is the difference between rest_route and wp-json in Wordpress REST API


I'm trying to build a WordPress plugin using the REST API instead of the complicated Setting API. According to the documentation, the REST API is built on top of the http://oursite.com/wp-json/ route. But the documentation also mentions this:

On sites without pretty permalinks, the route is instead added to the URL as the rest_route parameter. For the above example, the full URL would then be http://example.com/?rest_route=/wp/v2/posts/123.

Since ?rest_route works on both pretty and plain permalinks, I was thinking of sticking with ?rest_route instead of /wp-json.

So my question is: is there a difference between the two routes? What do you think guys?


Solution

  • The function get_rest_url() will support pretty permalinks and the rest_route query parameter, so it really doesn't matter.

    Pretty permalinks:

    get_rest_url( '/wp/v2/posts' ) === 'https://example.com/wp-json/wp/v2/posts'
    

    Not pretty:

    get_rest_url( '/wp/v2/posts' ) === 'https://example.com/?rest_route=/wp/v2/posts'