A helper class for using DarkSky.net in WordPress
Clone or download
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.gitignore Update .gitignore Jan 29, 2016
CHANGELOG.md version bump Aug 17, 2017
LICENSE Initial commit Jan 29, 2016
README.md version bump from 1.1.0 to 1.1.1 Jul 17, 2017
wp-darksky.php version bump Aug 17, 2017

README.md

WP-DarkSky

A helper class for using darksky.net in WordPress. Requires a latitude, longitude, and Dark Sky API Key. It's set up to use a transient by default, refreshed every 6 hours, to limit the number of API calls made in a day (the first 1,000/day are free).

See more about the API in the Dark Sky API Documentation.

Current Version: 1.1.1

Minimum PHP: PHP 5.3.0

Minimum WordPress: 3.1.0

Arguments

There are a few arguments available with this class to extend it further.

  • api_key This is required, sign up for one at https://darksky.net/dev/register
  • latitude Required.
  • longitude Required.
  • time Optional. Pass a timestamp for the forecast at a specific time.
  • cache_prefix The transient prefix, defaults to 'api_'. Note that the transient name is this prefix plus an md5 on the request url, so you're limited to an 8 character limit on the prefix or the transient will not save.
  • cache_enabled Boolean, default to true.
  • cache_time Time to store the transient in seconds, defaults to 6 hours.
  • clear_cache Boolean, default false. Set to true to force the cache to clear.
  • query An array of url query arguments, refer to the Dark Sky API documentation.

Example

You can call this class and output the data of the response fairly easily. Here's an example of a call to get the current day's temperature.

$args = array(
	'api_key' 	=> '', // Enter your API key
	'latitude'	=> '', // enter the longitude
	'longitude'	=> '', // enter the latitude
	'query'		=> array( 'units' => 'us', 'exclude' => 'flags' )
);
$forecast = new DarkSky\Forecast( $args );

// Get the current forecast data for the daily forecast, which provides the next 7 days
$daily = isset( $forecast->daily['data'] ) ? $forecast->daily['data'] : false;

// Pull out the current day's forecast
if( $daily ) {
	$date_format = 'n/j/Y';
	$time_now = date( $date_format, current_time( 'timestamp' ) );
	foreach( $daily as $day ) {
		if( isset( $day['time'] ) && $time_now == date( $date_format, $day['time'] ) ) {
			echo number_format( $day['temperatureMin'], 0 ) . ' / ' . number_format( $day['temperatureMax'], 0 );
			break;
		}
	}
}

The above will result with a min / max temperature forecast for the current day, similar to: 45 / 70 °. You can take any element of the Forecast response in a similar fashion and output into your site.

Want more?

See the collection of examples in this gist, as well as a basic Walkthrough in this post.

Weather Icons

Integrate Eric Flowers' awesome Weather Icons with this handy helper class.