Torbjorn Zetterlund

Mon 04 2016
Image

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):

  1. POST (Create)
  2. GET (Retrieve)
  3. PUT (Update)
  4. 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:

  1. Posts
  2. Pages
  3. Media
  4. Post Meta
  5. Post Revisions
  6. Comments
  7. Taxonomies
  8. Terms
  9. 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.

Advanced_REST_client2

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:

Snippets

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.

Screenshot_20160703-071539

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

Screenshot_20160704-204803

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

Screenshot_20160704-204827

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.

Advanced_REST_client3

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.

Share: