and like us on FB

Weather Underground, owned by The Weather Channel since 2012, is a commercial weather service that provides real-time weather information and reports for most major cities across the world… as well as providing services for newspapers and websites that syndicate their content. While the data is sourced mainly from official sources, they also aggregate feeds from users’ that upload their own localized data.

This is the first of a few articles detailing different ways of rendering weather forecasts. Since Weather Underground is a commercial service, we’ll follow up on this post detailing how to display weather details with the more permissive OpenWeatherMap .

The examples on this page are just that – examples. Don’t expect anything fancy. If you’re a client, keep in mind that if weather details are required we’ll always format the data to seamlessly integrate with your website. For the purposes of the examples we’ve rendered the results in text boxes.

The Result

The following is an example of a 4-day day/night forecast for Sydney, Australia (results are cached for 12 hours). Because defaults were defined as shortcode attributes, we’ve used just [weatherunderground] to return the result.

Tuesday

A mix of clouds and sun with gusty winds. High 23C. Winds NE at 25 to 40 km/h..

Tuesday Night

Partly cloudy skies. Low 16C. NNW winds at 25 to 40 km/h, decreasing to 10 to 15 km/h..

Wednesday

Cloudy skies early, then partly cloudy in the afternoon. High 27C. Winds N at 15 to 25 km/h.. Probability of rain: 10%.

Wednesday Night

Partly cloudy skies. Low 17C. Winds ENE at 10 to 15 km/h.. Probability of rain: 20%.

Thursday

Partly cloudy in the morning followed by scattered thunderstorms in the afternoon. High 23C. Winds S at 25 to 40 km/h. Chance of rain 50%.. Probability of rain: 50%.

Thursday Night

Thunderstorms during the evening will give way to steady rain overnight. Low 16C. Winds SSW at 25 to 40 km/h. Chance of rain 90%.. Probability of rain: 90%.

Friday

Rain early. A mix of sun and clouds in the afternoon. High 21C. Winds SSE at 25 to 40 km/h. Chance of rain 70%. Rainfall around 6mm.. Probability of rain: 70%.

Friday Night

Clear skies with a few passing clouds. Low near 15C. SE winds shifting to NW at 15 to 25 km/h.. Probability of rain: 10%.

Weather sourced from WeatherUnderground

If you were to include just the most recent forecast (either day or night), use [weatherunderground period="1"]. Each period essentially represents a half day.

Tuesday

A mix of clouds and sun with gusty winds. High 23C. Winds NE at 25 to 40 km/h..

Weather sourced from WeatherUnderground

WordPress Shortcode

Copy and paste the WordPress function into your theme's functions.php file or, if you sensibly have one installed, your custom functions plugin.

If you require shortcode to work in a sidebar widget, you'll have to enable the functionality with a filter. If you're using our custom functions plugin, you'll have that feature enabled by default.

Shortcode Attributes

apikey

An API key is required to use the WU service, and a payment plan is generally required for any commercial usage. Sign up here .

country

The JSON request for a specific location generally takes the form of Country(Region)/City(orLocation).json (with underscores instead of spaces). You may have to play around on the WU website to ensure you request weather in the correct format. The shortcode for a specific location is as follows: [weatherunderground country="Australia" city="Sydney"]

city

The JSON request for a specific location generally takes the form of Country(Region)/City(orLocation).json (with underscores instead of spaces). You may have to play around on the WU website to ensure you request weather in the correct format. The shortcode for a specific location is as follows: [weatherunderground country="Australia" city="Sydney"]

icons

A number of icons sets are available by default from the WU website . Alternatively, you can create your own… or use any of the sets created by others. The icons available at the time of writing are published here on WU’s website. Icons are referenced with the attribute of icons="a", icons="b", icons="c", icons="d", icons="e", icons="f", or icons="g", and so on (up until ‘k‘).

period

Each period is essentially a 12-hour block (or half day). So, to display the next 24 hours of weather we’ll use [weatherunderground period="2"]. WU generally returns an 8-period (or 4-day) block. If you’re planning on returning a single day forecast, it’d be prudent to render both half-days into the same container.

cache

The cache is the period of time to store the results of your query locally. I’ve used 12 hours but you might consider using a shorter timeframe so that you don’t miss out on new data when it becomes available.

PHP Function

Used outside of WordPress the following may be used. Usage of Simple Cache is required to avoid repeated requests to WU.

Considerations

  • Registration to the WU API is required before you can access data. The returned JSON data via a normal location request is over 1000 lines with enormous amounts of data (some of which you might consider using). For that reason, additional articles will be required to show how other data can be displayed in different ways.
  • Attribution with a link back to the original weather forecast is required as per WU’s terms and conditions.

Download

Title: Weather Underground Forecasts with WordPress or PHP
Description: Embed Weather Underground Forecasts with WordPress Shortcode or PHP. Requires WU API key.
Download: Shortcode (V0.2) | PHP Code (V0.2) | Plugin Page

Shortt URL for this post: http://shor.tt/2i2A