#143: Using and Caching Third-Party JSON with WordPress

Avatar of Chris Coyier
Chris Coyier on (Updated on )

📣 Freelancers, Developers, and Part-Time Agency Owners: Kickstart Your Own Digital Agency with UACADEMY Launch by UGURUS 📣

On the design of CSS-Tricks as I record this, one of the things I wanted to add was a “Front End Design & Development Jobs” widget, powered by the CodePen Job Board. Those jobs are available as JSON data.

Couldn’t we just do an Ajax request for that and use it? Perhaps a little JavaScript Templating? Sure, we could. But Ajax requests are async. I’m picturing a little spinner on every page load, waiting for the data to arrive and then filling in the jobs. Not quite the UX I’m wanting.

I want to do this with PHP so the jobs are there as part of the document as it comes down from the server. PHP (and WordPress) can totally make requests to third-party content just like an Ajax request can. In fact it’s even easier, since there aren’t cross-domain restrictions and JSONP type stuff involved.

But relying on a third-party request for the server to respond at all is a little risky. I wouldn’t want to require it for every page load on CSS-Tricks. Fortunately, that’s what WordPress Transients are for. In this video that’s exactly what we use. We go get the data every once in a while, then cache it locally so most requests are super fast local data.