Torbjorn Zetterlund

Sun 17 2016
Image

Android App for your WordPress Site with REST API

by bernt & torsten

Should you build a mobile app for your WordPress site or should you go with a responsive theme, this is a discussion that has split my WordPress developer friends.

A bunch of them stick with the notion of using a responsive theme design and design the site so it shows as a mobile app on a mobile device and shows as a web app in a web browser.

If you take a look at my website, if you open it on mobile it looks different then when you browse it on a laptop. This is all done with responsive design using HTML, CSS that should be sufficient for a basic WordPress blog.

Try my website on your mobile device and you find a different view than on a laptop. Here is how it looks like on a mobile device.

Mobile APP

Back to the other side of my WordPress developer friends, they say you should build an app for mobile devices, their argument is that with your own app you are shown as an image in the list of apps on your device, and that would give you more exposure and likelihood to get more followers.

So, I decided as an experiment, to build an Android app. I started out with defining the business requirements for the app, very basic – show categories, the latest posts and show the full posts from my WordPress site.

Next, I did a wireframe on how I wanted the mobile app to look like and then I started to develop the android app using Android Studio with Gradle build.

To get the categories, posts and detailed post data from my WordPress site, I used the WordPress REST API – to work with the REST API in Android I choose to use the Android Volley library.

Here is a good article – Android working with Volley Library if you are interested to learn how to use the Volley Library.

Setting things up

The first thing you need to do is install the REST API plugin on your WordPress site. After the REST API plugin is installed and activated, you can visit your site http://yoursite.com/wp-json/wp/v2/ to see the basic JSON object. It looks something like this:

{"namespace":"wp\/v2","routes":{"\/wp\/v2":{"namespace":"wp\/v2","methods":["GET"],"endpoints":[{"methods":["GET"],"args":{"namespace":{"required":false,"default":"wp\/v2"}

You can see elements of your site by appending the URL, such as http://yoursite.com/wp-json/wp/v2/posts/, or http://yoursite.com/wp-json/wp/v2/pages/.

That’s the data that we’ll use to pull into the mobile app.

The mobile app

This shows how my mobile app looks like.

Custom post types (CPT)

The Android app is simply going to display custom post types from WordPress. The only thing we are getting from the website is the data through the REST API.

To get custom post types, we can access

http://yoursite.com/wp-json/wp/v2/posts/types

I created a post type called “News” for this app, so I can access the data I need at

/wp-json/wp/v2/posts?type=news

My JSON data for the news CPT looks something like this:

 {
"id": 5836
"date": "2015-12-29T14:41:26"
"date_gmt": "2015-12-29T13:41:26"
"guid": {
"rendered": "https://torbjornzetterlund.com/?page_id=5836"
}-
"modified": "2015-12-29T14:41:26"
"modified_gmt": "2015-12-29T13:41:26"
"slug": "browse"
"type": "page"
"link": "https://torbjornzetterlund.com/browse/"
"title": {
"rendered": "Browse"
}...

If you don’t have a custom post type, that’s ok, you can access your posts at http://yoursite.com/wp-json/wp/v2/posts. Go ahead and visit that URL to view your JSON data.

That object gives us access to everything we need: title, excerpt, content, featured image, author, etc. We can now pull that data into our app, and display it any way we want.

Just compile your app with Android study and publish it on Google Play and you have your own Mobile App working with your WordPress site.

If you are interested in getting your own mobile app, just get in contact and I can help you with all aspects of developing a mobile app strategy for you.

Share: