What is REST API?
REST stands for REpresentational State Transfer and API stands for Application Programming Interface. The REST API provides an interface for applications to interact with wordpress websites by using HTTP protocols for sending and receiving data in the form of JSON (JavaSCript Object Notation) objects.
How does this sending and receiving data work in the REST API?
Client sends a request message to get access to data to the web server as HTTP Request and server response back with HTTP response to the Client.
HTTP methods:
HTTP methods are the action specified to get the requested resource from the server to the client. There are few HTTP methods listed below:
- GET : Used to fetch data which is a read-only operation.
- POST : Used to create new resources to the server.
- PUT : Used to update the existing resource or replace the resource to the server.
- DELETE : Used to Delete the resource from the server.
Which is basically called CRUD (CREATE, READ, UPDATE, DELETE) operations.
API uses endpoints to retrieve and manipulate wordpress content remotely without logging into the wordpress admin account.
What are Endpoints?
Endpoints are the URL that the API used to interact with web applications to the server.
How to create a Custom REST API in Wordpress website?
Let’s explore the steps to create a simple and efficient custom rest api for wordpress website.
First, let’s plan the API as to what action it needs to be performed.
Eg: In this blog, I am creating the REST api to get the blogs which are published in the wordpress website.
First step in creating the custom Rest API is to create and register the endpoint.
- Login to your wordpress Dashboard
- Go to the appearance > Theme File editor
- Click the file function.php and start coding there.
Step 1: Register the Endpoint. Here, declare a function and register the endpoint user register_rest_route() method with the arguments like
- $route_namespace called as namespace as ‘custom/v1’
- $route called path as ‘/blogs/’
- Within the array call the method called GET to fetch the data.
- Callback the function named ‘get_blog_posts’.
- Permission Callback refers to
- add_action() command tells perform the task/function.
// Register custom REST API endpoint
add_action('rest_api_init', 'blog_rest_api' );
function blog_rest_api() {
register_rest_route('custom/v1', '/blogs', [
'methods' => 'GET',
'callback' => 'get_blog_posts',
'permission_callback' => '__return_true', // Open to public
]);
}
Step 2: Write a code using a function to get post details.
1. Pass the arguments like “post type” which type you want to get, and status of the post which should be only published posts and the should not repeat the post content.
2. WP_Query: WP_Query is a class that is used to query the database of posts, pages and attachments.
function get_blog_posts($data) {
$args = [
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => -1, // Change to limit the number of posts
];
$query = new WP_Query($args);
$posts = [];
if ($query->have_posts()) { //checks whether it has any post.
while ($query->have_posts()) {
$query->the_post(); //for each and every post it return the information.
$posts[] = [
'id' => get_the_ID(),
'title' => get_the_title(),
'content' => apply_filters('the_content', get_the_content()),
'link' => get_permalink(),
];
}
}
wp_reset_postdata();
return rest_ensure_response($posts);
}
Write the whole code in function.php file in theme editor of wordpress dashboard. Once done, Save the file. After saving the code go to the url in the browser to check whether the code fetched the data of the blogs in wordpress website.
URL: https://sitename/wp-json/custom/v1/blogs
Sitename: url of the website domain eg. google.com
Route Namespace: inside the code part we declared the route namespace as “custom/v1”
Route: Route is what you declared in the code part as “/blogs/”
Postman:
You can also check your working status in postman. Postman is an API platform, where you can Prototype, document, test, and demo all your APIs in one place.
Set up the method which you need and paste the URL.
Example: Am using the same URL and giving the method as “GET”
URL: https://sitename/wp-json/custom/v1/blogs
It fetches the information the same as the data shown when we use the url in the browser. Likewise, You can use postman to test you API code for implementation.
Conclusion:
The REST API is one of the most underrated power tools in WordPress. If you need to integrate one application with another and need to fetch data from wordpress websites you can use REST API. Custom endpoints let you treat WordPress like a modern web app backend — and that’s a huge step up.
Let’s explore what our facility we have and modern techniques while using wordpress websites in future blogs. Stay Connected. Let’s catch you in another blog. Happy Learning!.
