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

4 Shares

This is article 3 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)

In the previous tutorials, we have created a basic plugin where we can write our code and learned how to extend REST API by modifying responses.

WordPress REST API has default routes and endpoints. It also allows developers to add custom routes and endpoints. That’s great. In this tutorial, we will learn how to add custom endpoints.

Recently, we have released a plugin called WP Restaurant Listings which you can use to add restaurant listing functionality to your WordPress site. This plugin doesn’t have builtin custom endpoints to access your restaurant site’s data through WP REST API. So, We are going to add custom endpoints in our plugin to access data by sending HTTP request easily.

We will use the controller pattern, so we can organize our code easily. You can learn more about the controller pattern here.

First, create a file called class-rest-tutorial-restaurant-endpoint.php inside the includes folder of the plugin we created in the previous tutorials with the following content.

We did not register the route yet. To register the new route we will use the register_rest_route function. We will pass in three parameters: the namespace, the route we want, and the options (an array of arrays for multiple methods) to this function. Inside the register_routes method, let’s add the following code.

We are using WP_REST_Server constants to set the method, it a common practice. So we are using WP_REST_Server::READABLE to set the method to ‘GET’. When users send ‘GET’ request to the registered route get_items method of the class will be called. Let’s add that method.

We also need to add get_items_permissions_check method. To make restaurants readable by all users we need to make sure that get_items_permissions_check return true.

Next, we are going to add a method which will return an array of parameters. Our newly registered route will accept these parameters when sending ‘GET’ request to WP REST API.

Finally, we need to add another method which will prepare restaurant data and return as an object.

Now, our class REST_TUTORIAL_Restaurnt_Endpoint is ready to do some cool things. We did not include this class yet, so we need to include it first. Add the following code inside the rest-api-tutorial.php file.

We are checking if the WP_Restaurant_Listings plugin is installed or not. If the the plugin is not installed and active we will not include the REST_TUTORIAL_Restaurnt_Endpoint class. So, it is required to install and activate WP_Restaurant_Listings plugin to make it work.

We did an awesome job. Now if we send “GET’ request to wp-json/rest-tutorial/v1/restaurants we will get the restaurants in simple JSON format as pictured below.

Similarly, we can register another route to get single restaurant item. We’re going to have the route match anything with /restaurants/{id}, where {id} is an integer. Check the following code.

We are using the get_items_permissions_check again to make restaurant readable by all users. Now, it’s time to retrieve the restaurant by adding the get_item method.

Now, you can now easily get a restaurant data by sending HTTP “GET” request to wp-json/rest-tutorial/v1/restaurants/{id} endpoint.

In the next tutorials, I will show you how to register route with POST, PUT, DELETE methods to update or delete a restaurant.

4 Shares
%d bloggers like this: