I am building a plugin for WordPress which will communicate with some remote REST API endpoints and display data in a list format in the public-facing view of WordPress.

Which method I should go for to call the API -

  1. Making request form server using wp_remote_get()?

    Pros -

    A. I can give filter and action hook to change the basic HTML layout.

    B. I can use transient to cache data.

    Cons -

    A. It will be slower.

  2. By making a direct call to the API server from the browser using AJAX?

    Pros -

    A. It will be much faster.

    B. I can give a function to override to change the basic HTML layout.

    Cons -

    A. Can't provide Wordpress hooks.

  • Not following what precisely you mean by AJAX and why do you assume it to be faster. Comparing to relatively slow network speeds any difference in implementing code is typically insignificant. – Rarst May 22 '17 at 15:55
  • More specifically I meant calling the API directly from the browser using ajax. Browser to API call will be much faster than routing through server. – Sunny Batabyal May 22 '17 at 17:33

In general it is always better to get the information straight from the source.... but you have to think about security. Using the REST API client from a different domain will require disable CORS protections for that domain. The amount of risk related to that depends upon how tied are the domains (do they have same admins, same kind of private data and such). This should not be done lightly and requires thinking as it opens a new attack vector against the "main" site.

OTOH if the data can not be cached, it is just "stupid" to do two requests instead of one.

The right implementation is probably to "overload" the API end points on the "main" server with a different domain that do not share cookies, which will eliminate the need for CORS protection. (this obviously assumes that user authentication is not required for what you are trying to do with the API)

  • So, If I will go for directly calling the API then how can I give filter and action hooks, to change the default layout of the view created using the response? – Sunny Batabyal May 23 '17 at 5:49
  • You will need to edit your question and add context. Not sure why would you want filters right now – Mark Kaplun May 23 '17 at 5:56
  • Hi Mark, He want to give theme developer an option to change the layout of HTML, he is generating from API response(JSON). So by filter hook, any theme developer can change the HTML layout and can create there own layout. Please correct me if I am wrong. – Aftab May 23 '17 at 10:41
  • @Aftab, filtering things at php side is not the only way to change styling, probably not even the best way when html needs to be manipulated – Mark Kaplun May 23 '17 at 10:50
  • So what is the best way ? – Aftab May 23 '17 at 11:11

REST API includes a JavaScript/Backbone client library so you can go like this..

// Create a new post
var post = new wp.api.models.Post( { title: 'This is a test post' } );

// Load an existing post
var post = new wp.api.models.Post( { id: 1 } );

reference: https://developer.wordpress.org/rest-api/using-the-rest-api/backbone-javascript-client/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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