How to effectively using the WP REST API V2 with WordPress Android App
by bernt & torsten
To build an optimized WordPress Android app using the WP REST API – You need to reduce the number of Clients to server requests. The WordPress Rest API is not that flexible as you have to make several API requests from the WordPress Android app to get the content data, comments count and content image. I will explain how I did it, for my WordPress Android app in this article.
For the benefit of everyone reading this, the acronym API stands for Application Programming Interface. REST means Representational State Transfer. An API can be considered RESTful if it’s design/architecture subscribes to a specific set of constraints.
HTTP requests are often the way that you interact with a RESTful API. HTTP means HyperText Transfer Protocol. This protocol allows information to be shared between a client (cell phone, tablet, laptop, desktop, etc.) and a web-accessible server in a request-response protocol.
HTTP Requests
In an HTTP request, you need to define the type of action that you want to perform against a resource. There are four primary actions associated with an HTTP request (commonly referred to as CRUD):
- POST (Create)
- GET (Retrieve)
- PUT (Update)
- DELETE (Delete)
WordPress objects
The WP REST API allows you to “access your WordPress site’s data (resources) through an easy-to-use HTTP REST API”. In the WP REST API, the resources include the following WordPress objects:
- Posts
- Pages
- Media
- Post Meta
- Post Revisions
- Comments
- Taxonomies
- Terms
- Users
CRUD Action
With the WP REST API, you can perform any of the four CRUD actions against any of your WordPress site’s resources listed above.
A GET POST response from a WordPress server looks like this, just a subset.
That is a lot of information, there is a limitation, in that if you need to use any of the links in the response to get additional data you would need to make another client to server requests, you want to limit that.
The Limitations
When you build a WordPress Android app – you can get the post data – to get the authors Avatar you would need to make an additional client to server request, and that would be the same if you need to get comments or getting the image URL for attached media, there could be a lot of client to server requests. There is a way around it.
Highly Extensible
The good things are that the WP REST API is very extensible and provides ways to both add additional fields to resources and to add endpoints to the API. To add additional fields or add endpoints you would either need to build a plugin or you can use a plugin like Code Snippets.
Code Snippets
I prefer the Code Snippets plugin, as it makes it easy to add new endpoints or add additional fields without building and installing a plugin.
In my WordPress Android App to limited the client to the server request, I needed to extend these URLs.
- Author Avatar image URL
- Count of Comments
- Thumbnail Image URL
- Full image URL
To do that, I used Code Snippets, here are the snippets I created:
I will explain each one, the first one was to get
Add Image thumbnail URL
Here is the code, I extended the response by finding the image_src and thumbnail and added it to the post response data.
function my_rest_prepare_post( $data, $post, $request ) { $_data = $data->data; $thumbnail_id = get_post_thumbnail_id( $post->ID ); $thumbnail = wp_get_attachment_image_src( $thumbnail_id ); $big = wp_get_attachment_image_src( $thumbnail_id, full ); $_data['featured_image_thumbnail_url'] = $thumbnail[0]; $_data['featured_image_big_url'] = $big[0]; $data->data = $_data; return $data; } add_filter( 'rest_prepare_post', 'my_rest_prepare_post', 10, 3 );
In the Android APP
There is only one call to get the thumbnail image with the title, after adding the code snippets.
Author profile Avatar
I needed to get the authors avatar URL, it is used in the comment section, here is what I added.
function my_rest_author_prepare_post( $data, $post, $request ) { $_data = $data->data; $avatar = get_avatar_url ( get_the_author_meta( 'ID' ), 32 ); $_data['author_image_thumbnail_url'] = $avatar; $data->data = $_data; return $data; } add_filter( 'rest_prepare_post', 'my_rest_author_prepare_post', 10, 3 );
Here is how it looks
Comments count
My Android app has a feature that displays the comments, I needed to show in the article the number of comments – this is how I got the comments to count.
function my_rest_prepare_post_comments( $data, $post, $request ) { $args = array( 'post_id' => $post->ID, // use post_id, not post_ID 'count' => true, // return only the count 'status' => 'approve' ); $_data = $data->data; $comments = get_comments($args); $_data['comments_count'] = $comments; $data->data = $_data; return $data; } add_filter( 'rest_prepare_post', 'my_rest_prepare_post_comments', 10, 3 );
And here is how it looks in the Android APP
Put it all together
After adding the code snippets the WP API V2 response from the server looked like this, see the red arrows they show the new fields.
That’s it, hope this helped you out, If you are interested in the WordPress Android source code, you can buy a copy from me – just click the link button and checkout.
If you have any questions you can contact me or you can use the comment fields below.
The Importance of Creativity After 60
As we age, the importance of maintaining cognitive health becomes increasingly clear. For...
Why It’s Important to Exercise When You’re Over 60
Many of us find ourselves in a pickle as the years pile up. Once reliable sidekicks, our...
A Poem: The Last Time
You never know when it will be,
The last time you ski down slopes of snow,