plugin development - add pagination to wp_remote_get

admin2025-05-31  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( '' );
$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.

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/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.

Share Improve this question asked Mar 13, 2018 at 8:53 DazBaldwinDazBaldwin 1034 bronze badges 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. – Jacob Peattie Commented 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 Commented Mar 13, 2018 at 8:58
Add a comment  | 

1 Answer 1

Reset to default 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.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748632393a313664.html

最新回复(0)