Create WordPress REST API

Steps for create Wordpress REST API

STEPS FOR CREATE WORDPRESS REST API

  1. Download WordPress plugin for REST API from below link

    https://wordpress.org/plugins/rest-api/#description

  2. Download WordPress plugins for JSON API & JSON API user from below link https://wordpress.org/plugins/json-api/ https://wordpress.org/plugins/json-api-user/

  3. Login to WordPress admin panel through SITE_URL/wp-login.php Then in sidebar of dashboard

    1. Go to option ‘PLUGINS

    2. Click on Add New

    3. Upload Plugin

    4. upload downloaded plugin

    5. Install Now

    6. Active that plugin

  4. Then check below link for API (i.e, SITE_URL/wp-json/wp/v2/posts ) You will get list of posts which are added in website http://itechnotion.in/listify/wp-json/wp/v2/posts​

  5. There is list of APIs for getting different types of data (i.e, posts, categories, comments, etc.) That list I have attached at end of this document.

  6. If there is need to add extra fields which aren’t providing by default API, then you need to add custom function for adding extra fields in below file /wp-content/themes/themename/functions.php I have added some custom functions there for adding extra needed fields.

  7. And for contact_us form & add comment form, I have written two APIs there in /api folder.

LIST OF REST APIs

1) For getting all posts(GET method)

SITE_URL/wp-json/wp/v2/posts/ or

SITE_URL/wp-json/wp/v2/posts/?page=1

For fetching next posts

SITE_URL/wp-json/wp/v2/posts/?page=2

For getting limited posts(i.e, for getting only two records)

SITE_URL/wp-json/wp/v2/posts/?page=1&per_page=2

2) For getting post by post id(GET method)

SITE_URL/wp-json/wp/v2/posts/577

3) For getting all categories: (GET method)

SITE_URL/wp-json/wp/v2/categories

4) For getting category by category id: (GET method)

SITE_URL/wp-json/wp/v2/categories/162

5) For getting all posts by category id: (GET method)

SITE_URL/wp-json/wp/v2/posts?categories=162

6) Search posts by keyword: (GET method)

SITE_URL/wp-json/wp/v2/posts/?search=world

7) For getting comments for post: (GET method)

SITE_URL/wp-json/wp/v2/comments?post=553

8) Link for adding custom APIs for add comment & contact us:

http://itechnotion.in/wp-plugin/custom_api.zip

Download zip from above link and put in root directory of server then unzip it in root directory. It will add that custom API files in api directory.

9) Custom field adding

A. Download plugin from http://itechnotion.in/wp-plugin/WpNewsCustomAPI.zip

B. Setup Plugin on your website.

Last updated