How to create REST API in WordPress? Step by Step Complete Guide

How to create REST API in WordPress? Step by Step Complete Guide

WordPress has default REST API enabled, we could find it in any WordPress site using http://yoursitename/wp-json, it shows the Rest API’s GET requests.

If you are reading this post then probably you want to make your own custom REST API in WordPress.

As we know WordPress is an awesome platform for blogging and developing  Websites.

WordPress is fully Customizable and with the help of plugins could extend different features in our projects.

To develop a Custom REST API in our WordPress site we need to develop a plugin. 

So, let’s add a Custom REST API feature in our WordPress site.

Step 1: Create folder ( plugin name) in your wp-content/plugins

WordPress has its own directory structure; there are 3 major directories in WordPress which are wp-admin, wp-content and wp-includes.

So, we need to create our plugin at wp-content/plugins, where we create a folder with its appropriate plugin name like custom-api or else.

Step 2: Create file in your recently created folder.

The second Step is to create a file in the recently created folder, you could name the same as your folder.

After creating the file you could write some code on this file, i have displayed below.

		* Plugin Name: All in One API
		* Plugin URI:
		* Description: All in One API for Blog
		* Version: 1.0
		* Author: Your Name
		* Author URI@

// Exit if accessed directly
if (!defined('ABSPATH')) {
	die('Write Message what you want');

function ps_posts()
	$args = [
		'numberposts' => 99999,
		'posts_per_page' => '6',
		'post_type' => 'post',
		'category_name' => 'featured',

	$posts = get_posts($args);
	$data = [];
	$i = 0;
	foreach ($posts as $post) {
		$data[$i]['id'] = $post->ID;
		$data[$i]['title'] = $post->post_title;
		$data[$i]['permalink'] = get_the_permalink($post);
		$data[$i]['featured_image']['thumbnail'] =          get_the_post_thumbnail_url($post->ID, 'thumbnail');
		$data[$i]['featured_image']['medium'] = get_the_post_thumbnail_url($post->ID, 'medium');
		$data[$i]['featured_image']['large'] = get_the_post_thumbnail_url($post->ID, 'large');
	return $data;
add_action('rest_api_init', function () {
	register_rest_route('ps/v1', 'posts', [
		'methods' => 'GET',
		'callback' => 'ps_posts',


Step 3: Install the API 

After writing those codes in a file, you would check the plugins in your admin panel or Dashboard.

To see the GET Request you need to install and activate the plugin. After activating the plugin you could check REST API in JSON Format by typing

Copyright © 2020-2021 Engineer Jagat All rights reserved. Developed by Ravindra Rana