DEV Community

Mithun Biswas for upnrunn technologies

Posted on • Originally published at upnrunn.com on

How To Extend WP REST API From Your Custom Plugin: Part 4

This is article 4 of 5 in the series “How To Extend WP REST API From Your Custom Plugin”. In case you missed the previous tutorials check the list below:

    1. How To Extend WP REST API From Your Custom Plugin (Part 1)
    2. How To Extend WP REST API From Your Custom Plugin (Part 2)
    3. How To Extend WP REST API From Your Custom Plugin (Part 3)

We have created a controller class in the last tutorial to fetch restaurant items and a single restaurant item. A controller class helps to handle the functionality of an endpoint easily. You can learn more about the controller class by reading the REST API Handbook regarding Controller Classes.

In this tutorial, we will write a few controller methods to update or delete a single restaurant item. Make sure you have installed WP Restaurant Listings and the custom plugins we created in the last tutorials.

First, we will add a delete method to the existing route where we can send HTTP request to delete a restaurant item.

The delete method we have just added accepts one argument to choose whether to bypass trash and force deletion. However, we have to add two missing methods delete_item and delete_item_permissions_check also.

Now, our delete_item method is ready which is responsible for deleting a restaurant item. We will write logic in delete_item_permissions_check method to check if a given request has access to delete a restaurant item.

You have noticed that we are using the get_restaurant method, let us add this method also.

Finally, everything is ready to delete a restaurant item by sending a simple HTTP DELETE request.

Let’s send a HTTP DELETE request to wp-json/rest-tutorial/v1/restaurants/{id} endpoint. If the restaurant has been deleted successfully you will get the following response.

When sending a request using Postman make sure you have installed Basic Auth plugin for basic authentication.

You also can set param force=true to permanently delete item instead of moving to trash.

In the next tutorials, we will learn how to create and update a restaurant item by sending the HTTP POST method.

The post How To Extend WP REST API From Your Custom Plugin: Part 4 appeared first on upnrunn® | Build & Deploy Powerful Application.

Top comments (0)