Creating wordpress Rest Api without Using Any Plugin

compodoc

Today we are going to create REST API without using any plug-in on Wordpress.
One thing interesting about Wordpress is that it is easy to use and you can create any good looking website in with 1 hr.
But If you want to create mobile application for same website then you have to install official WP-REST-API plugin or any third party plugin. It looks easy in starting but if you want to scale then it becomes very tough to learn internals of plugin how it really works which is very hard thing to do.

I have used wordpress for my most the websites and mobile apps as backend. The best thing about wordpress is that you have to ready made admin panel So need to inventing the wheel again. Just go with and create you mobile app with hours.

One thing you have to note down is that If you want to add custom with post or categories then you can use ACF (Advanced Custom Field Plugin) for this purpose it is very easy and mature enough plus you will find enough tutorials about it on google and youtube.

So let us start creating Wordpress REST API without using any plugin.

First for create an Api.php file in folder or beside the wp-content,wp-admin folders. I have created this inside a folder name 'api'

The file which controls the whole wordpress is wordpress loader file named wp-load.php .If you get access to it you can make any kind of changes in it.
So we are going to use the same.

So inside 'api' folder I am going to create api.php file and whole code is like this

https://gist.github.com/CodeNextGen/21877629f7c4b326d6791f12660950c1

Usage example

http://www.codenextgen.com/bishnoinextgen/api/api.php?method=getAllCategories

You will find many method implemented inside the api.php file like for recent post you have RecentPosts method, To call this

http://www.codenextgen.com/bishnoinextgen/api/api.php?method=RecentPosts

Things to remember

1) You to put this script file on right place,I have created api folder and then placed it inside. and give reference to wp-load.php (see very first line)

2) If you are not getting any error in response like white screen try to install ACF plugin mentioned above.

I hope this is going to very helpful for you people.

In case of any queries,comment below. Thanks for being here.


Tagged with:
CodeNextGen
Written by Sanjeev Kumar

Get notifications about new posts on Twitter, RSS or Email.