How to make WordPress API?

How to make WordPress API?

API stands for Application Programming Interface, which is used to communicate from One System to another.

In this article, We are going to create a custom WordPress API Plugin that provides all posts in JSON Format of your Blog.

There are a number of plugins that provide WordPress API but I am going to make a WordPress Plugin for WordPress Rest API in the easiest way.

So, let’s create WordPress Rest API Plugin Step-by-Step :

Step 1: Create WordPress Rest API Directory of Plugin Name

The Very First Step is to create a Directory having the Name of your WordPress Rest API Plugin in the Plugins Directory of Your WordPress CMS.

For example, the plugin name could be like this my-rest-api

Step 2: Create WordPress Rest API File

After creating Rest API Plugin Directory, we have to create a file. So just create a file and put some code for WordPress Rest API.

        * Plugin Name: All in One Rest API
        * Plugin URI:
        * Description: All in One Rest API for Blog
        * Version: 1.0
        * Author: Ravindra Rana
        * 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',
    $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',

The Above Code is the Example of creating WordPress Rest API, in this Plugin, we have used different fields like Post ID, Title, Permalink, Featured Image with three different sizes like thumbnail, medium, and Large.

The URL is going to create like this,

Step 3: Install Your WordPress Rest API

After creating a file in your Plugin, the Plugin was almost ready to install so go to your Dashboard and install the Plugin and see the result.

URL maybe this for your Website:

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