Created
June 29, 2016 15:04
-
-
Save mohsinrasool/e89369942c946b3943b3186cc658125b to your computer and use it in GitHub Desktop.
WordPress shortcode to fetch and display weather forecast
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function weather_forecast_func( $atts ) { | |
$atts = shortcode_atts( array( | |
'city' => 'Kingston, ON' | |
), $atts ); | |
// delete_transient( 'weather_forecast' ); | |
if ( false === ( $weather_forecast = get_transient( 'weather_forecast' ) ) ) { | |
// $weather_forecast_raw = get_transient( 'weather_forecast_raw' ); | |
$city = urlencode($city); | |
$weather_forecast_raw = wp_remote_post( 'https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%20in%20(select%20woeid%20from%20geo.places(1)%20where%20text%3D%22'.$city.'%22)&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys'); | |
if($weather_forecast_raw['response']['code'] != 200) | |
return ''; | |
$wf = $weather_forecast_raw['body']; | |
set_transient( 'weather_forecast_raw', $weather_forecast_raw, 24 * HOUR_IN_SECONDS ); | |
$wf = json_decode($wf); | |
$channel = $wf->query->results->channel; | |
$forecast = $wf->query->results->channel->item->forecast; | |
$units = $wf->query->results->channel->units; | |
// print_r($wf); | |
ob_start(); | |
?> | |
<div class="row" id="weather_block"> | |
<div class="col-sm-5 col-sm-offset-6 col-md-3 col-md-offset-7"> | |
<div class="today"><?php echo date('l', strtotime($channel->item->condition->date)); ?></div> | |
<hr /> | |
<div class="row"> | |
<div class="col-md-6"> | |
<span class="location"><?php echo $channel->location->city ?>,<?php echo $channel->location->region ?> </span> | |
<div class="temperature"> | |
<?php echo FtoC($channel->item->condition->temp); ?>° | |
</div> | |
</div> | |
<div class="col-md-6"> | |
<img src="<?php echo get_template_directory_uri().'/images/weather/'.WeatherImageName($channel->item->condition->text); ?>" alt="<?php echo $channel->item->condition->text; ?>" class="img-responsive" /> | |
</div> | |
</div> | |
<hr /> | |
<div class="row"> | |
<div class="col-sm-6"> | |
<h4>Wind:</h4> | |
<span>Speed:</span> <?php echo $channel->wind->speed?> <?php echo $units->speed ?> <br/> | |
<span>Feels Like:</span> <?php echo FtoC($channel->wind->chill)?>°<br/> | |
<span>Direction:</span> <?php echo $channel->wind->direction?>° <br/> | |
<h4>Atmosphere:</h4> | |
<span>Humidity:</span> <?php echo $channel->atmosphere->humidity?>% <br/> | |
<span>Pressure:</span> <?php echo $channel->atmosphere->pressure?> <?php echo $units->pressure ?><br/> | |
<!-- <span>Rising:</span> <?php echo $channel->atmosphere->rising?> <br/> --> | |
<span>Visibility:</span> <?php echo $channel->atmosphere->visibility?> <?php echo $units->distance ?><br/> | |
</div> | |
<div class="col-sm-6"> | |
<br/> | |
<img src="<?php echo get_template_directory_uri(); ?>/images/weather/wind.png" alt="<?php echo $channel->wind->speed?> <?php echo $units->speed ?>" class="img-responsive" style="max-width: 100px;" /> | |
</div> | |
</div> | |
<div class="row forecast"> | |
<?php | |
$i=0; | |
foreach ($forecast as $day) { | |
?> | |
<div class="col-sm-2"> | |
<div class="inner"> | |
<h5><?php echo $day->day; ?></h5> | |
<img src="<?php echo get_template_directory_uri().'/images/weather/'.WeatherImageName($day->text); ?>" alt="<?php echo $day->text; ?>" class="img-responsive" /> | |
<div class="temp"> | |
<?php echo FtoC($day->high); ?>C | |
</div> | |
</div> | |
</div> | |
<?php | |
if(++$i >= 6) | |
break; | |
} | |
?> | |
</div> | |
</div> | |
</div> | |
<?php | |
$weather_forecast = ob_get_clean(); | |
set_transient( 'weather_forecast', $weather_forecast, 24 * HOUR_IN_SECONDS ); | |
} | |
return $weather_forecast; | |
} | |
add_shortcode( 'weather_forecast','weather_forecast_func' ); | |
function WeatherImageName($str) { | |
return strtolower(str_replace(' ', '_', $str)).'.png'; | |
} | |
function FtoC($temp) { | |
return ceil ( ($temp-32)/1.8 ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment