WordPress Rest API; WordPress has grown significantly in recent years, since its inception in 2003 when it was just a blogging platform for one of the most popular content management systems. He has matured enough to support most of the online audience.
When it comes to developing a CMS, an important part lies in the functionalities it presents and improves with the passage of time. One of the newest features is the REST API that allows other platforms to interact with WordPress Rest API read more about wordpress.
This functionality will be of great help to web developers who develop personality applications and systems integrated with WordPress. The mechanism is possible because, WordPress offers the ability to add and delete content from any website or client, without having to install WordPress Rest API on that website.
To get an overview of what the WordPress Rest API, implies, I will introduce you to the basic concepts of REST and JSON.
The beginnings of WordPress Rest API.
First of all, REST is an abbreviation for Representational State Transfer. REST is a type of architecture. It is necessary to understand its structure and concepts, because they are essential in the development of applications that use this type of architecture.
This architecture model helps to create and organize a distributed system. He sees the web as a distributed hypermedia application whose source links are transmitted through the exchange of resource status representations.
The foundation on which the REST architecture is built are the resources, which are essential for the web; the web is also called a resource-oriented.
As for WordPress Rest API, these resources are discrete entities just like pages, users, posts, custom posts, comments, and so on. To interact with resources, Uniform Resource Identifier (URIs) are used, which are resource identifiers.
The WP REST API supports several types of resources, such as: pages, comments, users, posts, custom post types, media, conditions, meta posts and revisions.
HTTP response codes
The response of a server following a request is made by returning a response that contains an HTTP response code. These codes are numbers with predefined meanings. For example, almost anyone who has access to the Internet knows the status code 404, which means that what the user is looking for cannot be found.
Good to know! The server response is dependent on the method used in the request or the type of the HTTP verb.
Among the most used HTTP response codes that are found when working with the WP REST API are:
- 200 – OK : This code shows that the request was successfully completed and that the server provided an answer; usually appears after a request of the type GET that has been successfully completed;
- 201 – Created : Usually it appears after a POST request that has been successfully completed;
- 400 – Bad Request : A request appears then it was sent with invalid parameters or something missing. It is usually found in PUT and POST type requests;
- 405 – Method not Allowed : Found when an HTTP verb was loaded in the request and was not supported by the resource. An example of this is when a user tries to update a read-only resource;
- 410 Gone : A resource has been moved to another location; in situations where an attempt is made to delete a source that is already deleted or has been moved to the trash;
- 500 – Internal Server Error : The request cannot be completed due to an unexpected situation;
- 501 – Not Implemented : The server does not support the functionality to complete the request, being common in situations where the server receives a request method that it does not recognize.
Checking these response codes and HTTP verbs is done in more detail when starting the project with WordPress Rest API.
With the help of the WordPress Rest API, you can perform CRUD operations. CRUD is an abbreviation for Create Read Update Delete. These operations are done on sources using HTTP and this is why REST uses limited HTTP requests:
- PUT – Used to update a source;
- GET – To read or retrieve a resource;
- HEAD – To verify the existence of a source;
- POST : Used to create a new resource;
- DELETE : To delete a resource;
- OPTIONS : Used to retrieve all verbs supported by a resource.
In a REST-full service, each of these verbs has a well-defined meaning. GET, POST, PUT and DELETE are part of the CRUD actions; in the sense that they recover, update, create and delete entities.
The OPTIONS and HEAD verbs help a client determine a source and what HTTP verbs are available for subsequent operations.
To successfully make these posts using the WP REST API, the following endpoints are used:
GET wp / v2 / posts – This will return a collection of all the posts; and when the next endpoint is triggered it returns to a certain entity. For example, a post that has an id of 100:
GET wp / v2 / posts / 100 – A POST request creates a new entity, while the PUT request replaces that entity with a new version.
PUT wp / v2 / posts / 100 – Such a request will update a post that has an id 100.
POST wp / v2 / posts – Used to create a new post.
A DELETE request removes a resource from the system. This, as well as the type of PUT request are repeatable, in the sense that they have the same effect on the system.
The RESTful service also offers two verbs HEAD and OPTIONS; which are very helpful in situations where a client wants to check what resources are available in the system and what actions are supported; having an overview of the actions to be taken.
Reasons to use the JSON for WordPress Rest API.
The combination of JSON and REST offers a mechanism that uses WordPress back-end to create solid applications. The best examples in this regard are mobile applications that require data exchange between device / client and server.
Thus, JSON offers an easy alternative to XML-based solutions, which take into account the limitations that bandwidth has when using mobile data.
When it comes to exchanging data on different platforms, which can be read by both humans and machines, JSON can be considered a global connector. It can be used without any problem with most programming languages, because JSON is a text-based format for storing data.
And with the help of API, the content of your WordPress site is not limited, it can be accessed by both customers and other sites. The reason is because the WordPress Rest API, exposes certain parts that belong to the internal functionalities, and the clients can interact with your site remotely, either to create new content or to update some information. You can also retrieve content from a WordPress Rest API site and publish it to another site.
The WP REST API can be used for:
- Single Page Applications (SPAs);
- Mobile applications;
- Integration with other server-side platforms, such as: Django, Ruby, .NET, etc;
- Custom admin panels for WordPress Rest API..