This is article 1 of 5 in the series “How To Extend WP REST API From Your Custom Plugin”
- How To Extend WP REST API From Your Custom Plugin (Part 1)
- How To Extend WP REST API From Your Custom Plugin (Part 2)
I hope you’ve heard about the WordPress rest API. WordPress rest API provides some endpoints which you can use to fetch data from your WordPress site.
Imagine that you are planning to create a mobile app for your WordPress blog. The job of the mobile app is to display your latest blog posts. But the problem is that when you make http request to receive recent posts, you will find many fields in the rest API response. You don’t need all of these fields for your app and sometimes you will need some extra fields which are not returned by default in API response.
POWERFUL FEATURES, SIMPLIFIED
Want More Leads, More Conversions, & Increase Profits for WooCommerce Store?
A toolkit for WooCommerce.
In the following screenshot you can see how WP REST API response looks like when I make a POST request using Postman.
So, what can you do in such a situation? Yeah, don’t be panic. it’s really easy extending WordPress rest API as your need. The purpose of this tutorial is to show you how to extend WordPress rest API. I will divide this tutorial into several parts, this will help you to move forward step by step.
Create A Plugin
Before we get started. First, I am going to create a basic plugin where I can save all of my codes. This is the best way to extend WordPress functionality or add unique features to your WordPress site.
To create a plugin create folder called “rest-api-tutorial” where we will save all of our plugin files. Inside that folder create a file “rest-api-tutorial.php” with the following code.
Now create another file called “class-rest-api-tutorial.php” in the includes/ folder using the following code. This is our main plugin class.
Although the plugin do nothing at this moment, you can activate the plugin from dashboard. Let’s activate the plugin.
Finally our plugin is ready. In the next part, I will explain how to modify WordPress rest API response from this custom plugin.