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.
Note: The WeatherUnderground API has undergone changes and we're yet to catch up. Code will be updated soon.
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.
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.
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
country
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
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
icons="a"
, icons="b"
, icons="c"
, icons="d"
, icons="e"
, icons="f"
, or icons="g"
, and so on (up until 'k
').period
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
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
Not currently available.