0

I'm building a plugin for a client and it requires me to use wp_remote_get to retrieve records from their server.

I have everything working but I can't seem to find any good examples of how you add pagination to a remote request.

This is what I'm doing:

$response = wp_remote_get( 'http://somedomain.com/my/api/route' );
$body = wp_remote_retrieve_body( $response );
$decoded_body = json_decode( $body, true );
//code to loop through decoded body

I'm assuming that there is some argument like per_page that can be used in the call to wp_remote_get but I've tried it and it hasn't worked as far as I can see.

Further, when I have the number of records per page, How do I go and implement the actual pagination functionality?

If anybody has any experience with doing this or has any links to working examples, I would be very thankful.

2
  • 2
    It's not something that's up to wp_remote_get(). The API you're requesting needs to support it. It would either have to specifically support a 'page' or 'offset' parameters that return a limited set of records. You should refer to its documentation, if there is any, or ask someone on their end. Mar 13, 2018 at 8:56
  • I'm actually writing the API side of things as well. I was just hoping to be able to extract that part of the logic to the plugin itself.
    – DazBaldwin
    Mar 13, 2018 at 8:58

1 Answer 1

0

OK so I was gearing up to do as suggested by @Jacob above and use the offset and limit clauses on the API server.

However, this really seemed like logic that should be part of the plugin that I'm making rather than the API queries. As such, I thought I'd look into whether any other good/feasable solutions existed.

I have settled on using the Dynatable Jquery Plugin. This plugin actually has a whole host of options available alongside the pagination feature including dynamic searching and sorting of tabular data (both of which I was expecting to have to implement manually).

However, The main reason I have gone for this solution is the ease of installation. All that was required for getting the plugin working (with all of the features) was as follows:

  1. wp_enqueue_script and wp_enqueue_style the files that are included in the download.
  2. Give my table and ID (id="my-table")
  3. Apply the dynatable In my custom scripts by using: $('#my-table').dynatable(); within a $(document).ready call.

It's worth noting that this plugin has a dependency on JQuery, this should be included when you enqueue the script. You also need to (should) declare your JQuery within your custom script using the convention:

(function($) {
    $(document).ready(function() {
       $('#my-table').dynatable();
    });
})(jQuery);

This ensures that the $ shorthand works and doesn't interfere with anything.

I'm currently using it with the default settings. This means it normalizes the data in the table and renders it back. Due to the time it's saved me, I'll probably look at localizing the script and work with the raw JSON response I have.

Hopefully this will help other developers shave off a portion of their work day.

4
  • 1
    I think you're off-base in thinking pagination should be managed by the client. The normal reason you'd use pagination is to limit the amount of data you're dealing with at one time, largely for performance reasons. Handling pagination in the client defeats the purpose. I would advise implementing some sort of count and offset on the API. Mar 14, 2018 at 12:10
  • Though I get what you're saying. Believe me the data set that I'm working with is HUGE. However, the plugin is actually only pulling in a small subset of the data. There are a few hundred rows of about 10 columns each. Implementing all of the features (pagination, search and sort would add pretty big overheads that are not ideal.
    – DazBaldwin
    Mar 14, 2018 at 12:16
  • How are you only pulling a subset of the data if the API is returning all of it? Mar 14, 2018 at 12:17
  • Why would the API return all of the data? It's returning the result of the queries I'm executing!
    – DazBaldwin
    Mar 14, 2018 at 12:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.