Creating a mobile app with WordPress REST API and React Native

Tutorial Difficulty Level    

WordPress can work as an excellent back-end platform for your next native app, especially if it is content-driven or an online shop. In this article, you will learn the foundations for building mobile apps with React Native and WordPress.

We’ll set up a WordPress website with some content which we’ll use as a datasource for our React Native mobile app. Since WordPress 4.7, we now have the ability to connect to any WordPress install through a preexisting REST API. This allows us to use WordPress as a backend so that we can build things like React Native apps that pull content from our WordPress site.

We’re going to build a React Native app called “Leadership-Quotes” that pulls images from a WordPress media library via the WP REST API and display a random leadership quote with each button click.

Let’s set up a WordPress website first.  The quickest way to do this is with the template WordPress Server (with WP-CLI) on https://xoa.comp.dkit.ie.

Once you have your WordPress website set up, you can verify the REST API is working by visiting the correct URL. eg: http://10.108.154.124/wp-json/wp/v2/media (see this tutorial if permalinks are not working for you but you can still visit http://10.108.154.124/?rest_route=/, for example).

If you see something like the above, and recognize some of the file names listed, then you’re good to go. That’s about it on the WordPress side for now.

Setting up a React Native project

The WP REST API allows for so much innovation with WordPress. Anyone can build whatever they’d like using whatever software they want and use WordPress as the backend. We’re going to use React Native. To keep this brief, brush up on your React Native skills with their getting started guide.

Following the getting started guide bootstraps our project with Create React Native App automatically. And if you’ve successfully followed their tutorial, you should also have Expo installed and ready to go. If not, grab your mobile device and install the Expo client app from your app library. You’ll be using Expo on your mobile device to test your app as you make changes.

In the Terminal, go to the location where you want this app installed for development, and run:

After this, you should see a QR code in your Terminal window. Now open up Expo on your mobile device and click the “Scan QR Code” option, and scan the code. Your phone should refresh to show something like this.

Let’s begin editing the files. Open the App.js file in your favorite text editor. This file has some default code which displays the screen above. Let’s take a look at it.

The two import lines extend the React codebase and get it ready for what we need. Let’s add a few more items to that second import.

Let’s delete the rest of the default code and then add our own below those import tags.

After saving this, check your device to see if it looks something like this (your image may be different):

If everything connected correctly, you should see an image from the WordPress media library on the screen with a blue button that says, “Next quote.” If you click the button, the same image loads over and over again. While it’s great that we’re connected, it’s not the ideal outcome. We want the app to display a random image from the media library with each click.

Adding customized endpoints to the WP REST API

In order to get our button to work, we need to gather all the media IDs and randomize them for each click of the button. Unfortunately there’s no API endpoint that allows us to just grab an array of media IDs. No problem, like everything, there’s a WordPress plugin for that! You can checkout the source code on GitHub if you want.

Once this is set up, you should be able to visit: [your-domain.com]/wp-json/media-ids/v1/get-all-media-ids to see the API result. It will look a bit like this:

Updating the app to use customized endpoints

Now we just need to update our react native app to utilize this new endpoint and make cacheable requests. First we’ll replace the REQUEST_URL variable with REQUEST_URL_BASE and add two more variables for the different endpoints we’ll be accessing:

Then we’ll need to add two new properties to the initial state of the app: cardIDs and currentID:

Next, we’ll add functions to populate those properties in the same App.js file:

You’ll notice that the last thing getAllIDs() does after it has retrieved the list of IDs and added them to the state is that it calls this.fetchData() – this is because fetchData() will need the cardIDsto already be available in the state before it can run. We’ll need to replace fetchData() with getAllIDs() in the componentDidMount()method to make sure that getAllIDs() runs first:

And then we’ll need to update fetchData() to run getRandID() and then use the random ID to fetch that media item:

Not much has changed in fetchData(), we’ve added the call to getRandID() at the beginning and then we’re using the output of that along with REQUEST_URL_BASE and POSTS_URL_PATH to access a specific post via the API with a url that is easily cacheable by a server-side caching system such as WP Super Cache or Varnish. Notice that we also removed the array number from responseData.guid.rendered.

Save all that out, and your app should refresh in Expo on your device. Start clicking the button and watch each new image display on screen in random order. You did it!

If you’d like to know more, and try a different project in the same area as this one, then check out Developing an App Using the REST API and WordPress by Tom Ewer.