How To Extend WP REST API From Your Custom Plugin: Part 3
Tags: blog
Published: 2019-06-17 08:19:34

---
title: How To Extend WP REST API From Your Custom Plugin: Part 3
published: true
tags: Blog
canonical_url: https://upnrunn.com/blog/2018/04/how-to-extend-wp-rest-api-from-your-custom-plugin-part-3/
---

 **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)](https://upnrunn.com/blog/2018/02/extend-wp-rest-api-custom-plugin-part-1/)
2. [How To Extend WP REST API From Your Custom Plugin (Part 2)](https://upnrunn.com/blog/2018/02/extend-wp-rest-api-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](https://wordpress.org/plugins/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](https://developer.wordpress.org/rest-api/extending-the-rest-api/adding-custom-endpoints/#the-controller-pattern).

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.

<script src="https://gist.github.com/bhoot-biswas/e667dcc7fa0f82020d59e89e69f0de97.js"></script>

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.

<script src="https://gist.github.com/bhoot-biswas/c9297db60646295046d6405a80ae3736.js"></script>

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.

<script src="https://gist.github.com/bhoot-biswas/5d3e1f4bd6265904247faf825c2e8024.js"></script>

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.

<script src="https://gist.github.com/bhoot-biswas/4649f59465ccdcc9b51277ead48d75d5.js"></script>

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.

<script src="https://gist.github.com/bhoot-biswas/551e4043455d9d67e33300859b0fc257.js"></script>

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

<script src="https://gist.github.com/bhoot-biswas/b3cf8b692fee373687e855ba2eaaf55c.js"></script>

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.

<script src="https://gist.github.com/bhoot-biswas/03fffb22f8cad0e994f43063a6ff3cad.js"></script>

We are checking if the [WP\_Restaurant\_Listings](https://wordpress.org/plugins/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](https://wordpress.org/plugins/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.

![](https://i0.wp.com/upnrunn.com/wp-content/uploads/2018/04/restaurants_data.png?resize=1130%2C602&ssl=1)

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.

<script src="https://gist.github.com/bhoot-biswas/40b081c541222875f4d2c66227eb7ff9.js"></script>

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.

<script src="https://gist.github.com/bhoot-biswas/1933067ba6f069fa8f562844952cf506.js"></script>

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.

The post [How To Extend WP REST API From Your Custom Plugin: Part 3](https://upnrunn.com/blog/2018/04/how-to-extend-wp-rest-api-from-your-custom-plugin-part-3/) appeared first on [upnrunn® | Build & Deploy Powerful Application](https://upnrunn.com).