Giter Site home page Giter Site logo

wp-darksky's Introduction

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.0

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.

wp-darksky's People

Contributors

joshuadavidnelson avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.