REST (Representational State Transfer) API is a software architectural style that determines how web services communicate with each other through HyperText Transfer Protocol.
Step 1: Familiarize Yourself With the Key Concepts of REST API
We’ll begin our WordPress REST API tutorial by explaining the key concepts and terms:
- Routes & endpoints — a route is a URL that you can map to different HTTP methods, while an endpoint is a connection between an individual HTTP method and a route. /wp-json/ is an example of a route, and it contains all corresponding endpoints.
- Requests — an instance of WP_REST_Request. It is used to store and retrieve information for the current request.
- Responses — provides the data you requested or returns an error to let you know what went wrong.
- Schema — shows you a list of all properties and input parameters that REST API can accept and return.
- Controller classes — the place where you manage REST API moving parts.
Step 2: Get to Know the Most Useful REST API Endpoints
In this part of the REST API tutorial, we’ll show you several handy REST API endpoints that you can test with your site:
- First of all, you’ll want to know how to construct an HTTP call to the REST API. The base of every WordPress REST API call is as follows:
http://yourdomain.com/wp-json/
- Then, you can test the connection by executing the curl command in your CLI:
curl -X OPTIONS -i http://yourdomain.com/wp-json/
You should be prompted with a successful HTTP message:
HTTP/1.1 200 OK Date: Wed, 23 Oct 2019 19:51:41 GMT Server: Apache/2.4.29 X-Robots-Tag: noindex Link: <http://yourdomain.com/wp-json/>; rel="https://api.w.org/" X-Content-Type-Options: nosniff Access-Control-Expose-Headers: X-WP-Total, X-WP-TotalPages Access-Control-Allow-Headers: Authorization, Content-Type Allow: GET Transfer-Encoding: chunked Content-Type: application/json; charset=UTF-8
- Next, you can rinse and repeat this command using several endpoints. This time, we’ll simply use the GET version of curl to grab a JSON list of your WordPress posts. To do that, you can use the following command:
curl -X GET -i http://yourdomain.com/wp-json/wp/v2/posts
Alternatively, you might want to try this command to check out all of your existing WordPress pages:
curl -X GET -i http://yourdomain.com/wp-json/wp/v2/pages
If you want to see more examples, WordPress REST API offers a reference handbook that contains a lot of useful endpoints.
Step 3: Learn the Basics of REST API Authentication
Some actions and data within the REST API are public, while others require you to log in as an administrator. However, since it’s REST API, there is nowhere to log in.
To get around this issue, you can authenticate yourself when making any call that requires administrative access, such as viewing unpublished content or updating a post.
For this tutorial, we’ll be using the WordPress REST API Basic Auth plugin. It is a simple developer-only plugin to help you learn the REST API, but it is not intended for live sites. Here’s how to install it:
- Download the WordPress REST API Basic Auth plugin.
- Log in to your WordPress Dashboard and go to Plugins -> Add New. Click on the Upload Plugin button and select the plugin’s zip file.
- Go to the Installed Plugins menu and activate the plugin from there.
- Once Basic Auth is installed, open CLI and authenticate an API request by utilizing the user flag. Here is an example of how to apply the user authentication method, using curl to view unpublished posts:
curl -X GET --user username:password -i http://yourdomain.com/wp-json/wp/v2/posts?status=draft
Now that you get the hang of basic authentication, you can explore other recommended methods in the REST API documentation.
Step 4: Select Your First WordPress Post With the REST API
Once you understand how to make basic calls to the REST API using the curl command, you can proceed with selecting a specific post:
- First, list out all your posts as we did previously:
curl -X GET -i http://yourdomain.com/wp-json/wp/v2/posts
- Find the ID of a post you’d like to update. You’ll need to add an ID to the end of your query in order to select an individual post:
curl -X GET -i http://yourdomain.com/wp-json/wp/v2/posts/<ID>
You can use this command to pick a given ID for any REST API endpoint, whether it’s a post, page, or taxonomy.
Step 5: Update Your First WordPress Post With the REST API
Finally, let’s try sending an update to your selected post. For this REST API tutorial, we’ll try to rename our post’s title by using the POST command. Don’t forget to include the authentication credentials.
New changes will be shared using the d flag at the end of our command.
- In this example, you’ll pass a custom JavaScript object variable (title) to a custom value (My New Title):
curl -X POST --user username:password http://yourdomain.com/wp-json/wp/v2/posts/PostID -d '{"title":"My New Title"}'
Be sure to replace the username, password, post ID, and title name with your own WordPress details. - Then, you can reselect the individual post to verify the new changes:
curl -X GET -i http://yourdomain.com/wp-json/wp/v2/posts/PostID
Congratulations! You have just made your first administrative edits using the WordPress REST API.
Exampele: https://templateeye.com/blog/wp-json/wp/v2/posts/