Join Morten Rand-Hendriksen for an in-depth discussion in this video Some useful routes to remember, part of WordPress: REST API.
- [Narrator] To provide you with a quick reference for the basic routes in the WordPress REST API, I've created a collection of routes you can import directly into Postman. To do so, go to Postman, click Import in the top left-hand corner, click Choose Files and navigate to the exercise files for this course, then to the Assets folder and here you'll find the file WP REST API postman collection. Select that file and open it. And the collection gets added in under the Collections tab. Here I've compiled the 11 main routes for easy reference, including the REST API root and the index.
If you select any of these options, you'll notice that the URL is a little bit different. In place of the top level domain, you have these curly brackets and the words base URL. I set up all these routes using a variable for the domain name so you can slot in your own URL and use the same route across multiple different domains. To get all that to work, you need to set up some environments inside Postman. And here's how that's done. First, click on the cogwheel up in the top right-hand corner and click Manage Environments then add a new environment, we'll call this one restful dot dev.
Inside that environment, set up a key value pair where the key base URL and the value the actual top level domain for the site you're setting up. In my case that would be http://restful.dev. Then click Add, and now we can set up an additional domain. I'll set up more10 dot com. Again base URL, https://mor10.com. Add and close. So here's how this works.
Let's say I want to see all the posts from my current local site. First, I select posts here in my REST API collection. Then I go up to environment and select restful dev and click send. Now I get all the latest posts from my locally installed site on my computer. If that works, and I also want to test this on a live site on the web, all I have to do is go back up to environments and switch the environment to mor10.com, click Send again, and this time the response is from mor10.com.
This goes for all 11 of these main endpoints. So anytime I want to work on a site, I first select the site in my environment selector, then go to the WP REST API collection and select the route I want to use. Now, before we continue, there's one important thing you need to know. The URLs generated by these values in the collection, are not permanent. That means, if I go in here to, say, the Posts one, and I type in Posts four five six, and then run it. Then select Pages and then select Posts again, you'll see I actually changed the entry in my collection.
Now it's no longer posts, it's posts four five six. Here's how I recommend you use this collection. Any time you're looking for a route and you can't remember what it is or you just want to explore what comes out of a specific route, go to the collection and select that route. If you find what you're looking for and you're going to make a change to the URL for that route, copy the URL, create a new tab in Postman, paste it in, and then make your change here. It'll work the exact same way but you're no longer changing the entry in the collection.
And if everything fails and you make an accidental change to any of the items in the collection, just go back to the exercise files, import a collection again, and everything will be reset to the way it was.
- Using a REST client to communicate with a REST API
- Reviewing available routes, endpoints, and arguments
- Adding custom post types and taxonomies
- Altering the API response
- Using PHP for resiliency
- Passing info using wp_localize_script()
- Setting up a REST API request through AJAX
- Outputting selected post data to the front end
- Formatting post data to match a theme
- Triggering AJAX for an infinite scroll effect
- Using AJAX to load subsequent responses