Join Morten Rand-Hendriksen for an in-depth discussion in this video Communicating with the REST API using a REST client, part of WordPress: REST API.
- [Teacher] The purpose of the WordPress REST API is that we'll be able to send a request to the database through the API and then get the database content back in a compact easy to read format, all by sending a URL along with a verb. The REST API and all its endpoints sit at your WordPress site under a common URL. Yoursite.com/wp-json. From here we can request any of the content on the site. So for example, if I want the ten most recent posts from my site which sits on mor10.com, I can simply go to my browser, which passes a GET request and then say GET mor10.com/wp-json/wp/v2/posts.
You can see it here, has this guy with a jetpack on it flying. Click Add to Chrome. This will add the app to your Chrome Installation and once it's installed, it'll show up here under Chrome Apps. Then you can open Postman. It opens in a separate window. Here you can choose if you want to sign in and it'll store all of your sessions and you can share them between different computers. If you don't want to do that, just click Skip this, go straight to the app, and from here, we can make that same exact request. So you see here we have a GET request. I'll put an https://mor10.com/wp-json/wp/v2/posts.
Click Send, and now we get that same data that we did before, but this time, it's structured in a way that's easy to read. And here you see the actual structure of the json file separated down into key and value pairs. What we have right now are the ten most recent posts in my site. It starts out with each post being its own object, and that object has an ID, the publishing date, the date in Greenwich mean time, if it was modified and when it was modified, the URL slug, the post type, the link to the post, the title, the content and so on and so on and so on.
This response gives you the full post content as it sits in the database, and from here, we can take any one of these elements, wrap some HTML and CSS around it, and then display it on the front end in the browser. Now that you see how this works, try it out on your own local site or a site that you have live on the web and you'll see that you get the same type of data in Postman. This is one of the truly powerful features of the WordPress REST API. Once you know how to gain access to it, you can just replace the top level domain reference and query any site, local on your computer or live on the web and get a full json response.
- 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