Craigs Portfolio

6. WordPress Rest API

What is the WordPress Rest API?

Simply put, it is an interface which allows a developer to access WordPress from outside the WordPress installation.  As an example, I can access my WordPress website using another website in a different language and make and use posts/blogs as if it were a headless CMS.  This purely means that I can use a WordPress backend without using WordPress on the frontend.  The example is posts but it is not limited to posts however, it is what I will be using it for.

To test that the WP REST API is enabled, I do this by sending a HEAD or GET request to the server. HEAD request is more efficient method as the GET contains response body as well as the headers.  As shown below, the HEAD request is successful and it returns a Link on the bottom line which tells me that /wp-json is enabled.  I can use this to see what options are available either by inputting it into a browser or on Postman.

To be able to see what options I have available using the WP REST API, I can do this by sending an OPTIONS HTTP request.  I do so below for /wp-json/wp/v2/posts to see what methods are returned and the arguments for each method.

This returns objects which shows that this request belongs to the /wp/v2 namespace and the supported methods are GET and POST.  Below each of these methods has arguments which allows me to request these arguments to show this type of data.  The above screenshot shows some of the arguments for the GET request.  For the GET request I can use context.description to show that description if it is available for the post.  

The only 2 requests shown at this route is the POST and GET requests as it is the only options available for this route.  If I were to look at the OPTION request for an actual post id then I would find more options as I am targeting an existing post route.

The methods within this object are now completely different to the previous route as I now have additional options available for update and delete.  POST/PUT/PATCH all share the same arguments as expected as a create post will have the same arguments available as a update post.  Editing a post is the same thing as creating it, in logic standards.

The Delete method has different arguments as shown below.  The id of the post needs to be identified for it to be deleted and a force delete is an argument which can be applied.  A force delete will remove the post forever bypassing the trash bin.  This is not recommended for a page/post as most of the time it is requested by a client who often changes their mind. Most of the time sticking it into the trash or editing the “publish” argument to draft will suffice.

The only time I would be required to force a hard delete would be if I was using the WP API to delete something that cannot be trashed, such as a user.  An DELETE request for a user without a force argument will fail as a user cannot be placed into the trash.  There will be other examples of this within WP as well.

Space background with blue and red nebula
Dynamic Web Technologies

12. Demo Feedback

522 Error on OMDB Following the 522 error I was getting on the OMDB when searching, I tested the test data on the OMDB website

Read More »
Dynamic Web Technologies

11. Conclusion

Conclusion Prior to Dynamic Web Technologies course I wasn’t interested in JSON and no idea what AJAX even was.  Whenever JSON or AJAX comes up

Read More »
Dynamic Web Technologies

10. CRUD – jQuery

Read – jQuery For jQuery consumption of the WordPress Rest API, I am using Bootstrap framework offline. I have created a quick Bootstrap layout and

Read More »