How to use WordPress’ WTI Like Post in Android?

First things first, I have a plug-in in my WordPress website called “WTI Like Post” which is a thumbs up plugin for voting posts…

Here’s a preview from its GitHub repository:

Read More

WTI Like Post preview

If you click on these thumbs, an ajax link will fire up and vote the post, then it redirects back to the post page again. That ajax link is something like this:

http://example.com/wp-admin/admin-ajax.php?action=wti_like_post_process_vote&task=like&post_id=108&nonce=0d8d1f993f

Now, I want to use these links manually! Meaning that I want to vote a post by copy and pasting that link in a new tab (And not by clicking on buttons). The problem is that when I paste the link myself, my browser returns an error:
“The page isn’t redirecting properly”.

I’m making an Android app for my website, and the only way I can think to vote a post, is that link.

How can I use it manually? And more importantly, how can I use it with Android’s HttpURLConnection?

Related posts

1 comment

  1. Looking at the JavaScript source it would seem the Ajax link should be http://example.com/wp-content/plugins/wti-like-post/wti_like.php, with a POST body of action=wti_like_post_process_vote&task=like&post_id=108&nonce=0d8d1f993f. Thus, on Android, using the example code here, this should work:

    String urlParameters  = "action=wti_like_post_process_vote&task=like&post_id=108";
    byte[] postData       = urlParameters.getBytes( StandardCharsets.UTF_8 );
    int    postDataLength = postData.length;
    String request        = "http://example.com/wp-content/plugins/wti-like-post/wti_like.php";
    URL    url            = new URL( request );
    HttpURLConnection conn= (HttpURLConnection) url.openConnection();           
    conn.setDoOutput( true );
    conn.setInstanceFollowRedirects( false );
    conn.setRequestMethod( "POST" );
    conn.setRequestProperty( "Content-Type", "application/x-www-form-urlencoded"); 
    conn.setRequestProperty( "charset", "utf-8");
    conn.setRequestProperty( "Content-Length", Integer.toString( postDataLength ));
    conn.setUseCaches( false );
    try( DataOutputStream wr = new DataOutputStream( conn.getOutputStream())) {
       wr.write( postData );
    }
    

    Note that the nonce is not necessary on Android as setUseCaches(false) will ensure that the link is followed afresh every time.

Comments are closed.