Computers and the Internet

Weather Forecast in WordPress

As part of my working to put together a portal for my house I wanted to be able to include the current weather. now my favorite weather site, Weather Underground, includes ‘stickers’ which I could use to embed info on my site, I didn’t like them because none of them looked right on my site and non of them included all the information that I wanted. So I started looking for other ways to embed the info I wanted. However, while working on another related project, I discovered that the Weather Underground API provides all the information that I could want.
After some research and code hacing I put together the following PHP code. This fetches the required data via the API, pulls out the requested item and inserts it into our page.

$json_string = file_get_contents(“[API KEY]/conditions/q/IA/32907.json”);
$parsed_json = json_decode($json_string);
$icon_url = $parsed_json->{‘current_observation’}->{‘icon_url’};
$temp_f = $parsed_json->{‘current_observation’}->{‘temp_f’};
$feelslike_f = $parsed_json->{‘current_observation’}->{‘feelslike_f’};
$relative_humidity = $parsed_json->{‘current_observation’}->{‘relative_humidity’};
$pressure_mb = $parsed_json->{‘current_observation’}->{‘pressure_mb’};
$wind_string = $parsed_json->{‘current_observation’}->{‘wind_string’};

echo ‘<img src=”‘ . ${icon_url} . ‘” align=”right” />’;
echo “Current temperature: ${temp_f}\n”;
echo “Feels Like: ${feelslike_f}\n”;
echo “\n”;
echo “Relative Humidity: {$relative_humidity}\n”;
echo “Pressure is: {$pressure_mb}\n”;
echo “Wind: {$wind_string}\n”;

As you can see this code is easy to figure out and modify to customize what you want to see.
It is worth noting that this hits the Weather Underground API every time that the page is loaded / reloaded so on a busy site you may risk hitting your API call limits. As I am running this on something that s intended for my own personal use I am able to keep it withen the limits for the free API account. see Weather Underground for more Information on their API and it’s limits.


Jedite83 is a professional geek-of-all-trades and founder of Hacker Labs - The Geek and Otaku Blog.