Home Aviation Embed Weather Underground Forecasts with WordPress Shortcode or PHP

Embed Weather Underground Forecasts with WordPress Shortcode or PHP

0
Weather Underground

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 adjusted 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

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

■ ■ ■

Interested in WordPress and Web Design? Subscribe to our list and we'll send you subscriber-only tools and plugins

Short URL for this post: http://shor.tt/19oG

LEAVE A REPLY

Please enter your comment!
Please enter your name here