React Native App with WordPress backend – Part 1

WordPress can prove to be most cost effective and fast to built back-end for some specific type of react native apps.  Unfortunately, its quite underrated. With every new version, WP team is making there rest api more powerful and easy to implement. Before WordPress version 4.7 a plugin need to be installed in WP website to use the rest-api feature, but now, it has become integral part of WordPress.

WordPress rest-api has many pre-built endpoints but it also offers feature to create custom endpoints. Many-a-times we will need to create a custom endpoint when we are using WP as back-end for an App. So, in this post I will show you how we can create custom endpoints for React Native App.

We will create the end point as a separate plugin, so that, no other plugin or theme get affected by our code and they can be updated without any issue.

First thing we need to do is that, we will install WordPress version 4.7 or above on our local host or web host.

Next, we need to create a directory in the plugin directory of WordPress, of which name should should be same as the name of the plugin.

Now, create a file in the new directory with same name as directory and extension php.

Now add the following info in the new file :

The above commented information in required to create any WordPress plugin and you can change and customize it. Now we are ready with simplest type of WP plugin. You can go to menu Plugins>>Installed Plugins and find your plugin listed there. Go ahead and activate your new plugin.  At the moment plugin has no functionality because we haven’t added any code to it.

Now lets add the code below to create a new endpoint:

In the above code, we have created a handler, inside which we have added query to retrieve all post of which author id matches the id sent as parameter. Next if the $post variable is empty we are returning null otherwise we are returning post_title field of first element of array object.

Next we are attaching the handler to a rest_api_init hook of type action. rest_api_init hook is the type of hook that allows us to create custom rest api endpoint.

Now if you go to below url:

you will get the respective post title.

Now, lets change our query to fetch some custom data from the db. To make things short  I am using ListingPro theme  which creates and add some custom posts and taxonomy and we will write query to fetch it.

Below is the code to fetch all custom categories( taxonomy ) :

In the above code we are fetching all the Main/parent categories ( taxonomy ) of type ‘listing-category’. if you now go to url :

You will get all the Main categories of  ‘listing-category’ type.

By now you must have got idea of how to create custom endpoints. You just have to learn the WordPress Api/Queries to retrieve data and you can fetch any required data for you App.  Lets me show two more queries before we wrap-up this article.

Below is the code to fetch all sub-categories of type ‘listing-category’ of given category Id :

Below is the code to fetch all listing (custom posts) of type ‘listing’ of given category Id is 14 and category (taxonomy) type is ‘listing-category’ :

I shared the above queries because it took me lots of  time to find them and most of the queries available online are outdated. So I hope someone will find it time saving and useful. In next part we will use the above end-points in our app to create some useful functionality.

If you enjoyed the above article and found it useful then please subscribe to our newsletter and You-tube  channel and stay tuned to such informative article and videos.

Video Tutoial





One thought on “React Native App with WordPress backend – Part 1”

Comments are closed.