With the second-half of the REST API merge confirmation coming to WordPress 4.7, it’s safe to say that there are going to be there clearly be more than one WordPress REST API example made available in the next few months.
Then again, there are examples that already exist because it’s possible to include the REST API in your project as a plugin, but I digress.
One of the neater, smaller examples that I’ve recently come across is by Brian Krogsgard.
Not only does it show a WordPress REST API example, it shows how to use it in the context of a Rect applications.
Details on the REST API
Before actually getting into the example (and I know, that’s what you’re here for), I want to clarify the difference in what’s been available since WordPress 4.4 and what’s coming in WordPress 4.7.
So first, here’s the quick rundown of what’s been available in WordPress since 4.4:
For client authors, this doesn’t help you much right now. Hold tight, the team is working as fast as we can on the endpoints to get them ready for a future release. In the meantime, you can make sure sites you want to work with have the plugin installed, which isn’t a change from the current state.
For plugin and theme authors, you can start building new APIs immediately using the infrastructure now in core. This can start replacing your existing custom
admin-ajaxendpoints or other bespoke code you already have.
And here’s what’s coming in 4.7:
This infrastructure is the core of the REST API, and provides the external interface to send and receive RESTful HTTP requests. Since shipping in 4.4, the infrastructure is now used by WordPress Core for oEmbed responses, and by plugins like WooCommerce and Jetpack, enabling anyone to create their own REST API endpoints.
It’s exciting, right? And endpoints that will be exposed is significant. You can read more about that here.
Regardless, it’s important to understand the significance of the merge and how it affects those of us who make a living by building solutions on WordPress.
A WordPress REST API Example in React
Specifically, from the example mentioned, Brian states:
In an attempt to show as simple of a React app as possible, I am stealing Chris Coyier’s example that shows CodePen jobs. I wanted to show an example using WordPress post data.
And I love this approach because the goal shows something:
- with a small amount of code,
- and how it can practically be applied in another application.
You can see the CodePen for the example:
As mentioned it’s a simple example with enough code to show a good looking React-based application that serves as a WordPress REST API example, as well.
With WordPress 4.7 due in December, there’s enough time to get started with simple examples like this, more complex examples, or even reading through the available documentation.