Skip to content

Instantly share code, notes, and snippets.

@zacombs
Last active September 14, 2023 08:53
Show Gist options
  • Save zacombs/622d4504765d43bcbe1c9486d5f666fb to your computer and use it in GitHub Desktop.
Save zacombs/622d4504765d43bcbe1c9486d5f666fb to your computer and use it in GitHub Desktop.
Display weather on WordPress site using Weather Map API and Wordpress Transients API
/**
* Place these functions in functions.php of your WordPress theme. Then call them anywhere on your site.
*
* Be sure to replace the API Key, you can get this free from: https://openweathermap.org/api
*
* Transients are stored in the DB in the wp_options table. More info about transients: https://codex.wordpress.org/Transients_API
*
*
* Get the current weather conditions for Zip Code 40391 using the Open Weather Map API.
*/
function show_weather() {
// Do we have this information in our transients already?
$transient = get_transient( 'show_weather');
// Yep! Just return it and we're done.
if( ! empty( $transient )){
echo $transient;
}
// Nope! We gotta make a call to the API.
else {
$url = 'http://api.openweathermap.org/data/2.5/weather?zip=40391,us&units=imperial&APPID= ## API KEY HERE ## ';
$response = wp_remote_get( $url );
// Take the JSON and select the weather icon info. Pull the image from Open Weather Map using the icon info as the filename.
if( is_array($response) ) {
$body = $response['body']; // use the content
$resp = json_decode($body);
$weather = $resp->weather[0]->icon;
$icon = '<img src="http://openweathermap.org/img/w/' . $weather . '.png">';
}
// Save the API response so we don't have to call again for 60 seconds. API limits to 60 calls per minute.
set_transient( 'show_weather', $icon, MINUTE_IN_SECONDS );
}
// Return the icon. The function will return here the first time it is run, and then once again, each time the transient expires.
echo $icon;
}
/**
* Get the current temperature for Zip Code 40391 using the Open Weather Map API.
*/
function show_temp() {
// Do we have this information in our transients already?
$transient = get_transient( 'show_temp');
// Yep! Just return it and we're done.
if( ! empty( $transient )){
echo $transient;
}
// Nope! We gotta make a call to the API.
else {
$url = 'http://api.openweathermap.org/data/2.5/weather?zip=40391,us&units=imperial&APPID= ## API KEY HERE ## ';
$response = wp_remote_get( $url );
// Take the JSON and select the temperature info. Round the temperature up or down.
if( is_array($response) ) {
$body = $response['body']; // use the content
$resp = json_decode($body);
$basetemp = $resp->main->temp;
$temp = round($basetemp);
}
// Save the API response so we don't have to call again for 60 seconds. API limits to 60 calls per minute.
set_transient( 'show_temp', $temp, MINUTE_IN_SECONDS );
}
// Return the temperature. The function will return here the first time it is run, and then once again, each time the transient expires.
echo $temp;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment