The purpose of this helper class is to: perform the API call, process and return the JSON response, and store that response for quick reuse. Check out here. Feel free to drop it into a custom plugin for use on your site or your projects.

How it Works

Using the great HTTP API functions in WordPress, the helper class grabs the API response. It’s then stored in a transient (by default, but this can be overridden – see below), allowing us to store the response for quick recall later. This is important from a page load standpoint, of course, but also critical with services like Dark Sky, which provides 1,000 free calls per day.

It makes sense to grab the forecast once for a set period of time, rather than at every page load. If the latter is true, you could end up with quite a lot of calls to the API and quickly reach your free limit.

For my purposes I only needed the daily forecast, so a 6-hour cache is sufficient (it could be set to 24, but 6 hours allows for 4 API calls a day and accounts for potential changes in the forecast). If you needed, for instance, the current weather, then you could easily reduce the transient time to 5 minutes and still keep your calls below the 1,000/day limit.

Need a fresh of the API call? Just use the get_response function with the first parameter set to true clear the cache and get a fresh API response:

'cache_prefix'=>'api_', // careful here, md5 is used on the request url to generate the transient name. You are limited to an 8 character prefix before the combined total exceeds the transient name limit

Your email address will not be published. Required fields are marked *

Comment

You may use these HTML tags and attributes: <a href="" title=""> <b> <blockquote cite=""> <cite> <em> <i> <strike> <strong> . If you'd like to include code in your post, please post it to http://gist.github.com and include a link.

Name *

Email *

Website

About Me

I'm a WordPress Engineer. I build sleek, custom websites with WordPress and Genesis.