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. 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

  3. 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/wp-news/wp-json/wp/v2/posts

  4. 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.

  5. 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.

  6. 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

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

http://itechnotion.in/wp-plugin/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.

8) For contact us: (POST method)

SITE_URL/api/contact_us.php

Post parameters:

1) name

2) email

3) message

9) For adding comment: (POST method)

SITE_URL/api/comment.php

Post parameters:

1) post_id // 617

2) comment_author // itech

3) comment_author_email // itech@test.com

4) comment_content // test comment

5) user_id

// If adding comment without login then pass zero as `user_id`

else pass value of `user_id`

10) Custom field adding

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

B. Setup Plugin on your website.

Last updated