Home Aviation Weather Forecasts with OpenWeatherMap in WordPress or PHP

Weather Forecasts with OpenWeatherMap in WordPress or PHP

0

OpenWeatherMap is an online service that provides weather data, including current weather data, forecasts, and historical data to the developers of web services and mobile applications. For data sources, it utilizes meteorological broadcast services, raw data from airport weather stations, raw data from radar stations, and raw data from other official weather stations. What was once a very open service is slowly becoming more and more commercial with access to formerly free forecasts now incurring a fee. That said, they provide an enormous amount of data via an API that has only a few limitations. It is a brilliant service and ideal alternative services such as WeatherUnderground.

This post will show you how to include a basic day forecast in WordPress or with PHP. The previous version of this code (when published on Internoetics) dealt with a weekly forecast; that API enpoint is now relegated to the paid section so isn’t available unless you pay a monthly subscription.

The Result

The result rendered below is just an example (if you need something specific please let us know). The result is built with dodgy inline CSS and doesn’t really do much other than display 5 columns of daily data. Shortcode used is [bmowm_forecast location="sydney,au"]. The code also supports location by city ID ([bmowm_forecast location="2147714"]). Result is returned for Sydney, Australia.


23rd 8:00pm
Temp : 17.9°C
QNH : 1034hPa
RH : 92%
clear sky
Wind: SEbS 4kt

23rd 11:00pm
Temp : 15.9°C
QNH : 1034hPa
RH : 100%
clear sky
Wind: SbE 4kt

24th 2:00am
Temp : 15°C
QNH : 1033hPa
RH : 100%
scattered clouds
Wind: SbW 3kt

24th 5:00am
Temp : 16.2°C
QNH : 1032hPa
RH : 100%
light rain
Wind: SWbS 4kt

24th 8:00am
Temp : 15.6°C
QNH : 1033hPa
RH : 100%
clear sky
Wind: SW 4kt

Weather supplied by OpenWeatherMap.org

We cache results with Simple Cache for two hours (data is updated only every three hours). Usage of Simple Cache is required, and it’s used an alternative to the WP transient functions simply so we can retrieve older (cached) data if the API is unresponsive.

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.

The OpenWeatherMap API documentation describes the use of various parameters to alter returned data (for example, returning data in imperial or metric units). We’ve used the default JSON response and corrected temperatures (from Kelvin).

Rather than provide wind direction in degrees we’ve applied another function (below) to return the intercardinal compass abbreviation (described in more detail here). The function is reproduced below.

Shortcode Attributes

The following attributes are available for use in your shortcode.

apikey

The API is required and may be obtained for free from the OWM website.

location

The shortcode accepts either a city/country combination (often just the city), or city ID. The API also accepts latitude and longitude or zipcode – neither of which was included in the shortcode simply for the sake of simplicity. If you’re using our locations plugin, the latitude and longitude combination works well for an addition to the location landing page.

If you’re using a city/country pair, ensure you use ISO country codes and don’t use spaces.

offset

This if the GMT offset for the defined location (assuming you’re not returning the time as UTC).

version

The API version.

cache

The cache period. Again, Simple Cache is required.

PHP Function

Used outside of WordPress, the following function may be used.

Simple Cache and the beliefmedia_compass_cardinal() function are required.

Considerations

  • API information is available here . An API key is required.
  • Since data is obtained partially from an array of user weather stations, the information isn’t as reliable as information sourced from the Australian Bureau of Meteorology or the US National Weather Service.
  • Weather conditions and weather icons are listed here . While OWM permits hotlinking of their images, it’s not a bad idea to host them locally. Additional icon sets are floating around the web.
  • A full list of city codes (for use when referencing a city by location ID) is available here .
  • There are plenty of good OWM PHP libraries on the web. OWM lists this one hosted on Github.
  • We’ve used array_slice($weather['list'], 6, 5); on line 36 of the shortcode function. This extracts part of the array; in this case, starting at the 6th element with an offset of 5. You may alter this as required. If you return any more than 5 elements they’ll be rendered on a new row.
  • We have converted the default Kelvin temperature to Celsius on line 45. Alter as required for any other unit.
  • You may iterate over the returned weather array and return a multi-day forecast. This will likely be the subject of the next post relating to OWM.
  • If you were to use this code you will want to make a number of modification… starting with moving the styling to your custom functions CSS file.
  • The code is very basic and is provided only as an example. It needs work before it’s website-ready.

Download

Title: Open Weather Map (Day Forecast)
Description: Weather Forecasts with OpenWeatherMap in WordPress or PHP.
Download: Shortcode (V0.1) | PHP Code (V0.1) | Plugin Page

Shortt URL for this post: http://shor.tt/1XPb

LEAVE A REPLY

Please enter your comment!
Please enter your name here