0

I have two WordPress websites - Alpha and Beta. I have 3 sections on the Alpha site that display content from the Beta site using WordPress Rest API:

  • Section 1 displays latest posts from the Beta site.
  • Section 2 displays posts from category one from the Beta site.
  • Section 3 displays posts from category two from the Beta site.

Right now I'm using wp_remote_get to make REST API calls to the Beta site. Since PHP is a sequential language, the page load halts for a few seconds because it waits for all API calls complete before moving forward.

I was able to get around this issue by using WordPress Transients on Alpha to cache data I get from the Beta Site. But when the transient expires, page load halt thing happens again on the first page load.

I want load posts from the Beta site on the Alpha site asynchronously. I was trying to do it using AJAX but I'm unable to figure out how.

Edit: I have PHP function that renders HTML for post section. I'm trying to figure how do get data from the Beta site using jQuery/Ajax and pass it my PHP function so that it could render it.

  • 1
    And your question is? "How to do it with AJAX?" -> learn JavaScript. Using jQuery.getJSON() it isn't too hard – kero Oct 27 '17 at 11:14
  • How do I get fetch data using jQuery.getJSON() from the Beta site and pass it my PHP function so that it could render it? – Rahul Verma Oct 27 '17 at 11:20
1

Step 1: Learn how to use AJAX in Wordpress Plugins/Themes. The trick is that your Javascript doesn't get the Stuff from Beta Site, your Javascript calls a PHP-Function you coded that itself gets the Stuff.

Step 2: Keep using the Transients. Check within your Ajax Callback, if there is a transient. If there is, return it. If there isn't, get the stuff from the Beta site with wp_remote_get, save it to the transient, then return it to your Ajax function.

Step 3: Put the Call to your AJAX-Function into the document.ready function.

Step 4: Profit ;)

  • Exactly the answer I was looking for! Thank you so much for pointing me in the correct direction. I guess I knew how to use the tools, I just didn't know how to use together to solve this problem. – Rahul Verma Oct 31 '17 at 11:25
1

You are solving the wrong part of the problem here. Shifting the netwrok request to the JavaScript/client would just make it happen every single load. That makes performance issue worse, not better. Though delay would be presented in a different way.

What you really want to implement here is async refresh of the transient. When it expires the data should be retrieved and updated independently of user viewing the page.

There is a great TLC Transients library available that can do exactly that, on top of native WP transients.

  • Thank you! I didn't know the TLC Transients. I'll check it out. – Rahul Verma Oct 31 '17 at 11:15

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.