A Complete Guide to WordPress REST APIs For Beginners

Posted by admin on

When you want to learn more about WordPress, you'll certainly meet the term WordPress REST API.  This part of the WordPress core is proven to be able to make WordPress develop into a modern and dynamic platform. 


Well, in this article, we'll cover everything about the WordPress API.

Not only that, we'll also explain how it works and provide a basic guide to the WordPress API that's easy for beginners to understand.

What is the WordPress REST API?

Before we explain what a REST API is, you need to know what the API means first.

API or Application Programming Interface is an interface that can connect two applications to be able to communicate easily. It can be a website, mobile app, web app, or other. 

You can imagine this API as a waiter in a restaurant connecting guests and chefs in the kitchen.

Well, WordPress REST API is one type of API. Rest (Representational State Transfer) API utilizes connecting w software via HTTP in its data exchange process.

With the WordPress API, WordPress website development can be easier because developers don't need to create apps with the same functionality and purpose from scratch.

For example, you can install the Google Maps API on your website. You'll get all the features and data of Google Maps without having to create your own map app.

Well, an API must be eligible below in order to be considered RESTful:

  • Uniform interface — The URL used to access the system must be uniform, consistent, and accessible through common approaches such as GET.
  • Separate servers and clients — The goal is for both to be developed independently. In addition, if there are changes on the server side, the application can still be accessed in the same way.
  • Server condition unchanged — The server remains in the same state when there is a new request from the API because there is no need to store the request.
  • Cached — All resources must be cached for high performance. Both server and client side.
  • Layered system — Between client requests and authentication from the server, there are several layers separating them. Whether it's a layer of security, a cache layer, or a layer with other functions.

WP REST API is an API format that allows you to integrate various WordPress elements with external applications and servers using JSON. Therefore, you need to first understand the basics of WordPress, JavaScript and JSON.

Benefits of WordPress REST API

Here are some of the benefits of the WP REST API for WordPress as a whole:

  • Making WordPress look more modern and user-friendly. An example is the appearance of editor Gutenberg.
  • Become a more universal hub for different programming languages
  • Make WordPress development more flexible with simpler system standards
  • Developing a WordPress website is easier with simple content integration
  • It's easier than ever to get a wide selection of apps to use in WordPress

How the WordPress REST API Works

Before WordPress version 4.4, you had to use a plugin if you wanted to access the WordPress API. But now, the WordPress REST API has become a core part of WordPress. 

Well, to call the REST API, there are four types of HTTP methods that you can use, namely:
  • GET — Retrieves information from the server. For example, when visiting a wordPress login page, your browser will send a GET request to the server. Then, the server will process it using the API.
  • POST — Sends information to the server. For example, after successfully logging into the WordPress admin page, you will make a request to enter data in the form of post articles.
  • PUT — Used to edit and update existing data. For example, when you want to change your password.
  • DELETE — Can be used to delete information. For example, when you want to delete an account that's inside a WordPress admin.
Basic Guide to Using rest APIs

In this guide, we'll use Windows's Command Line Interface (CLI) Putty. If you're using MacOS and Linux, you can open Terminal.

As a first step, copy your server's IP address using the SSH command. However, we recommend you to use a demo WordPress site or localhost. If there is an error, let it not interfere with your main website.

1. Understand basic REST API commands

In this guide we'll use some REST API commands as follows:

  • Routes &ENDPOINT: Routes are URLs that you can map to various HTTP methods. While Endpoints are connections between HTTP. An example is /wp-json/ which contains all routes and endpoints.
  • Request: used to store and retrieve information from a WP_REST_Request code.
  • Responses: this command provides the data you requested or can also provide the location of the error message. That way, you know where the errors are.
  • Schema: displays a list of properties and input properties that can be accepted and returned by the REST API.
  • Controller classes: This command is the location where you manage the REST API.
2. Get to know ENDPOINT commands using the WordPress REST API

In this REST API guide, we'll use some ENDPOINT REST APIs that you can try:

First of all, you should call HTTP to the REST API by accessing the browser and enter the following link:

http://yourdomainname.com/wp-json/

Second, you can test the connection by running the curl command on your command line. To do this, use the following command:

curl -X OPTIONS -i http://yourdomainname.com/wp-json/

Next, you can repeat this command by using multiple ENDPOINTs. This time, we'll use curl's GET command to retrieve a list of JSON files from your WordPress posts. To perform the GET command, you can use the following command:

curl -X GET -i http://yourdomainname.com/wp-json/wp/v2/posts

Alternatively, you can also try this command to check all the pages on your WordPress website:

curl -X GET -i http://yourdomainname.com/wp-json/wp/v2/pages

Rest API Authentication basic guide

The data in the WordPress REST API is public. While some WordPress sites require you to log in as a WordPress administrator. Well, to solve this problem, you'll need to authenticate your WordPress account with the server. 

The goal is for you to be able to make any request as an administrator. Examples include viewing unpublished content or updating posts on the website. In this tutorial, we'll be using the WordPress REST API Plugin – OAuth. This plugin is a developer-only plugin that helps you learn about rest APIs.  But, this plugin is not intended for live or online websites. 

Here's the step:
  • Log in to your WordPress dashboard and go to the Plugins menu > Add New > search plugin "WP REST API – OAuth".
  • Then click Install and Activate.
  • Once the WordPress REST API is installed, open the CLI and authenticate to the REST API using your WordPress user. Here's an example of how to implement a user authentication method, using curl to view unpublished posts.
curl --user username_WordPress:Password_WordPress -i http://Domain_Anda.com/wp-json/wp/v2/posts?status=draft

Finding Blog Posts Using the WordPress REST API

Now you understand how to make a request to the REST API using the curl command. So, you can proceed with selecting a specific WordPress post using the following commands.

First, view all the posts inside your WordPress site using the following command:

curl -X GET -i http://Domain_Anda.com/wp-json/wp/v2/posts

Then, you will be given a JSON view as below screenshot shown.  Just select one of the post IDs you'll see.

Second, after you select the post ID, then enter the following command by adding the post ID at the end of the curl command:

curl -X GET -i http://Domain_Anda.com/wp-json/wp/v2/posts/ID

You can use the above command to select the post ID, category, page, comment, and others provided by ENDPOINT for the WordPress REST API.

Updating Post Titles Using rest APIs

Then, we'll update the post on WordPress using the REST API. 

In this guide, we'll try renaming the article title by using the POST command. So, make sure you add the post ID of the article to the POST command, yes!

Next, you can enter the following command on the command line. Don't forget to match the post ID and title of the article you want to change.

curl -X POST --user username_WordPress:password_WordPresss http://Nama_Domain.com/wp-json/wp/v2/posts/IDPost -d '{"title":"New Blog Title"}'

After that, make sure you've successfully changed the article title using the selected post ID. The trick is to check the post ID using the following command:

curl -X GET -i http://DomainAnda.com/wp-json/wp/v2/posts/PostID

And Congratulations! You've already successfully learned the basic guide to using the WordPress REST API.


Wow, it turns out that REST API has a myriad of benefits for WordPress websites? 

Not only does it make WordPress richer in features and content, wp rest API also makes WordPress development easier. So that developers can inject a variety of advanced features that make it easier to use WordPress as a whole.

Oh yes, although the guide in this article is just the basics, we hope it can still help you get to know the WordPress REST API more deeply. Especially from the way it works and using the basic commands on your own website.

Such is the discussion of the WordPress REST API guide for beginners. Hope this article is useful for you, yes! If you have any questions, feel free to leave a question in the comments field. Bye!

Previous
« Prev Post

No comments:

Post a Comment