WordPress REST API Tutorial – A Beginner’s Guide
Are you looking for a beginner-friendly WordPress REST API tutorial? Then you’ve come to the right place! In this article, we will introduce you to the WordPress REST API project, explain why it is such a big deal, and offer some insights on how to use it. There are many types of Application Programming Interfaces (API) right now, but REST stands out as a modern standard. It works by manipulating textual data from one place to another without direct access to a database or user interface. REST API is delivered via HyperText Transfer Protocol (HTTP) endpoints, using JavaScript Object Notation (JSON) formatting. These endpoints represent the posts, pages, and other WordPress data types. If you have never worked with JavaScript or its object notation before, we recommend learning about the basics of JSON first. It will help you better understand this WordPress REST API tutorial. Thanks to JSON formatting, WordPress REST API allows WordPress to exchange data with other websites and software written in any programming language. Hence, developers are not constrained to PHP anymore, and they can use WordPress to handle the data via REST API. The increasing focus on the API also raises arguments about the most important programming language to learn. Since the REST API is based on JavaScript, you may soon find that server-side JavaScript can replace PHP altogether. This notion is backed up by the fact that the new WordPress.com software, Calypso, runs entirely on JavaScript and REST API. Additionally, by standardizing the way applications interact with WordPress data, WordPress development will become simpler and faster. For that reason, our WordPress REST API tutorial is here to encourage you to pay more attention to this feature. In this WordPress REST API tutorial, we will be using the command-line interface (CLI) to execute all of the requests. CLI enables you to easily interact with the REST API without having to write additional scripts to request and process the information. The first thing you need to do is open a CLI program on your computer: Terminal for macOS and Linux, and PuTTY for Windows. After that, copy your shared or dedicated IP address and log in with your SSH credentials. We also recommend using a demo site or local testing for this tutorial. Furthermore, make sure that it runs on WordPress version 4.4 or higher as well. We’ll begin our WordPress REST API tutorial by explaining the key concepts and terms: In this part of the REST API tutorial, we’ll show you several handy REST API endpoints that you can test with your site: You should be prompted with a successful HTTP message: Alternatively, you might want to try this command to check out all of your existing WordPress pages: If you want to see more examples, WordPress REST API offers a reference handbook that contains a lot of useful endpoints. 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: Now that you get the hang of basic authentication, you can explore other recommended methods in the REST API documentation. Once you understand how to make basic calls to the REST API using the curl command, you can proceed with selecting a specific post: You can use this command to pick a given ID for any REST API endpoint, whether it’s a post, page, or taxonomy. 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. Be sure to replace the username, password, post ID, and title name with your own WordPress details. Congratulations! You have just made your first administrative edits using the WordPress REST API. REST API is a powerful addition to WordPress’ core and developers have begun to uncover its capabilities. Therefore, learning to work with it can improve your skills and enable you to create apps that use WordPress’ services. In this WordPress REST API tutorial, you have learned the five important steps to master this feature: While this WordPress REST API tutorial only scratches the surface of its capabilities, we think it’s still a good starting point before you get deeper into it. Do you have any questions? Let us know in the comment section below!
How the WordPress REST API Works
Why WordPress REST API Matters for Developers
5 Steps For Getting Started with the WordPress REST API
Step 1: Familiarize Yourself With the Key Concepts of REST API
Step 2: Get to Know the Most Useful REST API Endpoints
http://yourdomain.com/wp-json/
curl -X OPTIONS -i http://yourdomain.com/wp-json/
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
curl -X GET -i http://yourdomain.com/wp-json/wp/v2/posts
curl -X GET -i http://yourdomain.com/wp-json/wp/v2/pages
Step 3: Learn the Basics of REST API Authentication
curl -X GET --user username:password -i http://yourdomain.com/wp-json/wp/v2/posts?status=draft
Step 4: Select Your First WordPress Post With the REST API
curl -X GET -i http://yourdomain.com/wp-json/wp/v2/posts
curl -X GET -i http://yourdomain.com/wp-json/wp/v2/posts/<ID>
Step 5: Update Your First WordPress Post With the REST API
curl -X POST --user username:password http://yourdomain.com/wp-json/wp/v2/posts/PostID -d '{"title":"My New Title"}'
curl -X GET -i http://yourdomain.com/wp-json/wp/v2/posts/PostID
Conclusion