Join Morten Rand-Hendriksen for an in-depth discussion in this video Installing the WP-API, part of WordPress REST API (WP-API): First Look.
- As I'm recording this course, the WordPress rest API is still in BETA. That means, to get it to work on your WordPress site, you may have to install a plug in. Now that said, the plan is for the rest API to be baked into WordPress Core over the next two major releases. So, the Core of the rest API will be baked in in WordPress 4.4. And the reminder of the structure will be baked in in WordPress 4.5. That means, as of WordPress 4.5, you should have a fully baked rest API built into the Core of the application.
Before we install the rest API on our site, let me quickly run you through the different places on the web where you can find information about the API. The first place is the documentation. This is where you find information about how to interact with the rest API. That documentation is found at v2.wp-api.org. And it's been built out as the feature is being built up. So if you go to it right now, you'll see there's not that much information. But, once the rest API gets baked into Core, you'll see there'll be a lot more information here.
Next is the actual API project, which is hosted on GitHub. If you want to interact with the people who are currently building the API either by submitting issues, or maybe trying to do some poll requests. You can do so by going to github.com/WP-API /WP-API. Under the API account on GitHub, you'll also find some other tools including basic authentication, and other core features. But, like I said, currently the API is in BETA. And that means, if you wanted to run on your site, you need to install a plug in.
That plug in is called WordPress REST API (Version 2), and it's available from the WordPress.org plug in directory. The reason why it has the name Version 2 is because there used to be a different version of the WordPress rest API, and some sites are still using that version. So the original one, WordPress rest API, is also still available. But like I said, if you want to use it now, and you want to use something that will match what goes into WordPress Core, you should use Version 2. So, let's intall the WordPress API on a site.
Here I have a locally hosted site on my computer with just the theme unit test data installed. And to install the API I simply go to the back end. Go to Plugins, and Add New. Search for rest api. You'll see that, just like with everything else in WordPress there are lots of different plugins that are called REST API. Some of these hook on to the new rest API. Some of them are old APIs people built. But the one you want is this one, WordPress Rest API (Version 2) by the WP REST API Team.
So I'll install that. And activate it. And once activated, you'll notice that nothing changes within WordPress. But we can test to make sure it works now. Like going to the front end, and putting in the following URL. First you start with the root of the API itself, which is wp-jason. Then we have to put in the n point, which is wp/V2/posts/1. So this should give us the post and the system that has ID 1.
And that should be the Hello World post. Now when you do this in your browser, you are getting the get end point for this route. And the get end point gives us the Jason version of the first post. And in the browser, it looks pretty weird. And that's fine because we're going to process this later on. If you look closely here, you'll see here it says title, rendered, Hello world. And that's the standard first post that comes with all WordPress sites. So if this is what you get on the browser when you enter this route and this end point, it means the API is working.
Now, one more thing. There is a chance when you enter that route that you don't get anything or you get an error. That might be because of your permalinks. Because, for the WordPress API to work, you need to have a specific permalink setting in WordPress. To change this, go to Settings, and Permalinks. And here you want this one, Post name. This is the permalink setting that works with the rest API and once you set it, and you go back to the browser and enter that route one more time, you should get the same result as you saw in my browser.
With the WP API, developers can access the data on a WordPress site—such as users, posts, and categories—programmatically. The result is an expansion of the already limitless field of possibilities where theme and plugin development is concerned.
This course is a first look at the WP API: what it is, how to get it running, how to interact with it, what it can do for us, and what we can do with it. Join Morten Rand-Hendriksen, your guide and WordPress guru, as he walks through the tools and major features (such as routes and endpoints) of the WP API, as well as a short project that shows how this new API will be used in real-world workflows.
- Installing the WP API
- Exploring common routes
- Examining WP API output
- Building plugins with the WP API
- Crafting WP API URL requests
- Creating AJAX functions with the WP API