Beginner’s Guide to the WordPress REST API

Looking for a beginner WordPress REST API tutorial? Then you have come to the correct place! In this article, we’ll introduce you to the WordPress API REST project, explain why it’s so important, and give you some ideas on how to use it.

Introducing the WordPress REST API

The API project of Transfer of Representational Status (REST) ​​is a software architectural style that determines how web services communicate with each other via Hypertext Transfer Protocol.

In June 2013, Ryan McCue and Rachel Baker uploaded the REST API project to . After gaining much public support and attracting nearly 100 contributors for future enhancements, the project was added to the WordPress core at .

By now, almost every professional working with WordPress has heard of the REST API. However, since its main integration, only advanced developers have taken the time to learn how powerful this new feature can be. This is very unfortunate as this tool can open up endless possibilities to extend your WordPress website.

WordPress REST API aims to provide an API that can be integrated with themes, mobile apps, and more. It allows WordPress to interface with any application, and developers can even use it to create their own APIs.

Here are some ways you can take advantage of it in your projects, all from real life examples:

  1. uses the REST API to provide developer access to its internal infrastructure, making it possible to develop applications based on its services.
  2. a WordPress recipe plugin, allows developers to create their own recipe apps using their .

How the WordPress REST API works

There are many types of application programming interfaces (API), 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 APIs are delivered through endpoints of Hypertext Transfer Protocol (HTTP), using the JavaScript Object Notation (JSON) format. These endpoints represent WordPress posts, pages, and other types of data.

If you’ve never worked with or its object notation before, it’s a good idea to start by learning the . This will help you better understand this WordPress REST API tutorial.

Why the WordPress REST API is Important for Developers

Thanks to the JSON format, the WordPress REST API allows WordPress to exchange data with other websites and software written in any programming language. Therefore, developers are no longer limited to PHP, and can use WordPress to handle data through the REST API.

See also  WordPress Sitemap: What is it and how to create one?

The growing focus on the API signals a shift in which are the most relevant programming languages ​​to learn. Because the REST API is based on JavaScript, we may soon find that server-side JavaScript is the new PHP.

You can already see this in WordPress.com’s new software, , which runs entirely on JavaScript and the REST API.

Additionally, by standardizing the way applications interact with WordPress data, WordPress development will be simpler and more intuitive.

For that reason, our WordPress REST API tutorial is here to encourage you to pay more attention to this feature.

5 steps to get started with the WordPress REST API

In this WordPress REST API tutorial, we will use the command line interface (CLI) to execute all requests. The CLI allows you to easily interact with the REST API without having to write additional scripts to request and process the information.

The first thing to do is open a CLI program on your computer: Terminal for macOS and Linux, and for Windows. After that, copy your shared or dedicated IP address and login with your SSH credentials.

We also recommend using a local testing or demo site for this tutorial. Also, make sure it is running on WordPress version 4.4 or higher.

Step 1: Familiarize yourself with the key concepts of the REST API

We will start our WordPress REST API tutorial by explaining the key concepts and terms:

  • Routes Y end points. A route is a URL that you can assign 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 contains all corresponding endpoints.
  • petitions. An instance of WP_REST_Request. It is used to store and retrieve information for the current request.
  • Answers. They provide the data you requested or return an error that lets you know what went wrong.
  • schemes. Returns a list of all properties and input parameters that the REST API can accept and return.
  • controller classes. The place where you manage the moving parts of the REST API.

Step 2: Learn about the most useful REST API endpoints

In this part of the REST API tutorial, we’ll show you several useful REST API endpoints that you can try with your site:

  1. First, you’ll want to know how to build an HTTP call to the REST API. The basis of each WordPress REST API call is the following: http://yourdomain.com/wp-json/
  2. You can then test the connection by running the command curl in your CLI: curl -X OPTIONS -i http://yourdomain.com/wp-json/

    You should see 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: ; 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

  3. You can then repeat this command using several of the main endpoints. This time, we’ll simply use the GET version of curl to get a JSON list of your WordPress posts. To do so, you can use the following command: curl -X GET -i http://yourdomain.com/wp-json/wp/v2/posts

    Alternatively, try this to see 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, the WordPress REST API offers a plugin that contains many useful endpoints.

Step 3: Learn the REST API Authentication Basics

Some actions and data within the REST API are public, while others require you to log in as an administrator. However, since it’s the REST API, there’s nowhere to log in.

To work around this issue, you can authenticate yourself when making any calls that require administrative access, such as viewing unpublished content or updating a post.

For this tutorial, we will be using the WordPress REST API plugin. It’s a simple plugin for developers that helps you get to know the REST API, but it’s not intended for live sites. Here we explain how to install it:

  1. Download the WordPress REST API plugin.
  2. Login to your WordPress Dashboard and go to Plugins -> Add New. Click the button load plugin and select the plugin zip file.
  3. go to the menu Installed plugins and activate the plugin from there.
  4. Once Basic Auth is installed, open the CLI and authenticate an API request using the prompt user. 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 know about basic authentication, you can explore other recommended methods in the.

Step 4 – Select your first WordPress post with the REST API

Once you understand how to make basic REST API calls using the curl command, you can select a specific post:

  1. First, list all your posts like we did above: curl -X GET -i http://yourdomain.com/wp-json/wp/v2/posts
  2. Next, find the ID of a post you want to update. Add this ID to the end of your query to select just that individual post: curl -X GET -i http://yourdomain.com/wp-json/wp/v2/posts/

You can use this to select a certain ID for any REST API endpoint, whether you want to view a post, page, or taxonomy.

Step 5 – Update your first WordPress post with the REST API

Finally, let’s try to send an update to the selected post. For this REST API tutorial, we will try to rename our post title using the POST command. Don’t forget to include the authentication credentials.

New changes will be shared using the flag d at the end of our command.

  1. In this example, you will 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”}’

Make sure to replace the username, password, post ID, and title name with your own WordPress details.

  • You can then reselect the individual post to check for the new changes: curl -X GET -i http://yourdomain.com/wp-json/wp/v2/posts/PostID
  • Congratulations! You just made your first administrative edits with the WordPress REST API.

    conclusion

    The is a powerful addition to the core of WordPress, and few developers have begun to discover its capabilities. Consequently, getting on board now and learning how to work with it can make you more valuable as a developer, and will allow you to create applications using WordPress services.

    In this WordPress REST API tutorial, you have learned the five important steps to master this feature:

    1. Get familiar with the key concepts of the REST API.
    2. Learn about the most useful REST API endpoints.
    3. Learn the basics of REST API authentication.
    4. Select your first WordPress post with the REST API.
    5. Update a WordPress post with the REST API.

    While this WordPress REST API tutorial only scratches the surface of its capabilities, we think it’s still a good place to start before delving into it.

    Do you have any question? Let us know in the comments section below!

    Gustavo is passionate about creating websites. He focuses on the application of SEO strategies at for Spain and Latin America, as well as the creation of high-level content. When he is not applying new WordPress tricks you can find him playing the guitar, traveling or taking an online course.

    Loading Facebook Comments ...
    Loading Disqus Comments ...