Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Extending the WP-DarkSky helper class and adding some Weather Icons by Eric Flowers.
<?php
/**
* A helper class for that extends the WP-DarkSky helper class with Weather Icons.
*
* You'll need to add the Weather Icons stylesheet, web fonts, etc and enqueue them correctly.
*
* @see weathericons.io
* @see https://developer.wordpress.org/reference/functions/wp_enqueue_style/
* @see https://darksky.net/dev/docs/
*
* @link https://github.com/joshuadavidnelson/wp-darksky
*
* @since 1.0.0
* @since 1.1.0 - Update for new Dark Sky API (versus old Forecast.io naming)
*
* @author Joshua David Nelson, josh@joshuadnelson.com
*/
$args = array(
'api_key' => $api_key, // set your api key
'latitude' => $lat, // set your latitude
'longitude' => $long, // set your longitude
'query' => array( 'units' => 'us', 'exclude' => 'flags' )
);
$forecast = new DarkSky\Weather_Icon_Forecast( $args );
// Get the current forecast for a given latitude and longitude
$daily = isset( $forecast->daily['data'] ) ? $forecast->daily['data'] : '';
$current = '';
$title_attr = '';
if( is_array( $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'] ) ) {
// check the variables are setup
if( isset( $day['temperatureMin'], $day['temperatureMax'] ) && !empty( $day['temperatureMin'] ) && !empty( $day['temperatureMax'] ) ) {
// The temperature output
$current = number_format( $day['temperatureMin'], 0 ) . ' / ' . number_format( $day['temperatureMax'], 0 );
// Set the icon, if there is one
$icon = !empty( $day['icon'] ) ? $forecast->get_icon( esc_attr( $day['icon'] ) ) : '';
// set the title attribute to the forecast summary
$title_attr = !empty( $day['summary'] ) ? ' title="' . esc_attr( $day['summary'] ) . '"' : '';
}
}
}
}
// If we have a valid forecast, we can build the widget
if( ! empty( $current ) )
echo '<div class="widget weather-widget"><h5 class="title" id="weather-widget-title">Today\'s Weather</h5>' . $icon . '<span class="temp">' . $current . ' &deg;</span></div>';
<?php
/**
* A helper class for that extends the WP-DarkSky helper class with Weather Icons.
*
* You'll need to add the Weather Icons stylesheet, web fonts, etc and enqueue them correctly.
*
* @see weathericons.io
* @see https://developer.wordpress.org/reference/functions/wp_enqueue_style/
* @see https://darksky.net/dev/docs/
*
* @link https://github.com/joshuadavidnelson/wp-darksky
*
* @since 1.0.0
* @since 1.1.0 - Update for new Dark Sky API (versus old Forecast.io naming)
*
* @author Joshua David Nelson, josh@joshuadnelson.com
*/
namespace DarkSky;
class Weather_Icon_Forecast extends Forecast {
/**
* Weather Icons mapped for Forecast.io.
*
* @since 1.0.0
*
* @var string
*/
private $icons = array(
'clear-day' => 'wi-forecast-io-clear-day',
'clear-night' => 'wi-forecast-io-clear-night',
'rain' => 'wi-forecast-io-rain',
'snow' => 'wi-forecast-io-snow',
'sleet' => 'wi-forecast-io-sleet',
'strong-wind' => 'wi-forecast-io-wind',
'fog' => 'wi-forecast-io-fog',
'cloudy' => 'wi-forecast-io-cloudy',
'partly-cloudy-day' => 'wi-forecast-io-partly-cloudy-day',
'partly-cloudy-night' => 'wi-forecast-io-partly-cloudy-night',
'hail' => 'wi-forecast-io-hail',
'thunderstorm' => 'wi-forecast-io-thunderstorm',
'tornado' => 'wi-forecast-io-tornado',
);
/**
* Build it.
*
* @uses wp_array_slice_assoc()
* @uses wp_parse_args()
*
* @since 1.0.0
*
* @param array $args The array of arguments.
*/
public function __construct( $args = array() ) {
parent::__construct( $args );
}
/**
* Grab a weather icon.
*
* @since 1.0.0
*
* @param string $icon_name The Dark Sky icon name.
*
* @return string $icon Icon print out or empty string.
*/
public function get_icon( $icon_name ) {
if( array_key_exists( esc_attr( $icon_name ), $this->icons ) ) {
return '<i class="wi ' . $this->icons[ esc_attr( $icon_name ) ] . '"></i>';
} else {
return '';
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.