https://torbjornzetterlund.com/wp-content/uploads/2015/12/wordpress-logo-notext-rgb-960x600_c.png

How to effectively using the WP REST API V2 with WordPress Android App


To build an optimized WordPress Android app using the WP REST API – You need to reduce the number of Client to server requests. The WordPress Rest API is not that flexible as you have to make several API request from 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 its design/architecture subscribes to a specific set of constraints.

HTTP requests are often the way that you interact with a RESTful API. HTTP means Hyper Text 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.

In a 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 any HTTP request (commonly referred to as CRUD):

  1. POST (Create)
  2. GET (Retrieve)
  3. PUT (Update)
  4. DELETE (Delete)

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

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 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 alot of client to server requests. There is a way around it.

Highly Extensible

The good things is 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.

I prefer Code Snippets plugin, as it makes it easy to add new endpoint or add additional fields without building and installing a plugin.

In my WordPress Android App to limited the client to 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 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, want to try out the Torbjorn Zetterlund app that uses WP REST API and get some ideas on how to build your own WordPress Android app, you can download it from here. en_app_rgb_wo_45

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.

[contact-form][contact-field label=’Name’ type=’name’ required=’1’/][contact-field label=’Email’ type=’email’ required=’1’/][contact-field label=’Website’ type=’url’/][contact-field label=’Comment’ type=’textarea’ required=’1’/][/contact-form]

4 Responses

  • Collins on June 28, 2017, 18:15:16

    i would like to know how to post comments on my wordpress site from my android app… please any help as regards this would be realy appreciated. thank you

    Reply to Collins
    • torbjornzetterlund on July 13, 2017, 13:31:35

      In the android code you need to add a constant – something like this
      public static final String URL_COMMENTS_LIST_PAGE = "https://example.com/wp-json/wp/v2/comments?post=_STORY_ID_";

      Next step will be in your code to add the fields you need to apply to the url parameters

      protected Map getParams() {
      Map
      params = new HashMap();
      params.put("author_name", name);
      params.put("author_email", email);
      params.put("content", comment);
      return params;
      }

      That should give you an url like this to post

      https://example.com/wp-json/wp/v2/comments?post=_STORY_ID_
      &author_name=Your%20Name%20Here
      &[email protected]
      &content=Your%20Comment%20Here

      I hope this helps

      Reply to torbjornzetterlund
  • antok on August 8, 2017, 09:59:08

    what u have this app but using ionic ?

    Reply to antok

Menu