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 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.
Temp : 11.5°C
QNH : 1019hPa
RH : 58%
Wind: SW 15kt
Temp : 10.2°C
QNH : 1020hPa
RH : 60%
Wind: SWbW 14kt
Temp : 9°C
QNH : 1020hPa
RH : 63%
Wind: SWbW 10kt
Temp : 8.1°C
QNH : 1022hPa
RH : 65%
Wind: WSW 8kt
Temp : 12°C
QNH : 1022hPa
RH : 49%
Wind: WbS 8kt
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.
Copy and paste the WordPress function into your theme's
functions.php file or, if you sensibly have one installed, your custom functions plugin.
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.
The following attributes are available for use in your shortcode.
The API is required and may be obtained for free from the OWM website.
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.
This if the GMT offset for the defined location (assuming you're not returning the time as UTC).
The API version.
The cache period. Again, Simple Cache is required.
Used outside of WordPress, the following function may be used.
Simple Cache and the beliefmedia_compass_cardinal() function are required.
- 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.