Home Aviation Embed Weather Underground Forecasts with WordPress Shortcode or PHP

Embed Weather Underground Forecasts with WordPress Shortcode or PHP

0

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.

Saturday

Showers possible. Low 16C.. Probability of rain: 60%.

Saturday Night

Considerable cloudiness with occasional rain showers. Low 16C. Winds SE at 15 to 30 km/h. Chance of rain 70%.. Probability of rain: 60%.

Sunday

Partly cloudy skies in the morning will give way to cloudy skies during the afternoon. High around 20C. Winds SE at 15 to 30 km/h.. Probability of rain: 20%.

Sunday Night

A few clouds from time to time. Low around 15C. E winds shifting to NNW at 15 to 25 km/h.. Probability of rain: 10%.

Monday

Sunshine and clouds mixed. High 21C. Winds E at 15 to 30 km/h..

Monday Night

A mostly clear sky. Low near 15C. Winds N at 15 to 30 km/h.. Probability of rain: 10%.

Tuesday

Sunny, along with a few afternoon clouds. High 23C. Winds NNE at 25 to 40 km/h..

Tuesday Night

Partly cloudy during the evening. A few showers developing later during the night. Low 18C. Winds NNE at 25 to 40 km/h. Chance of rain 30%.. Probability of rain: 30%.

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.

Saturday

Showers possible. Low 16C.. Probability of rain: 60%.

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/2nPv

LEAVE A REPLY

Please enter your comment!
Please enter your name here