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.

9th 5:00am
Temp : 21.4°C
QNH : 1011hPa
RH : 86%
broken clouds
Wind: NbE 6kt

9th 8:00am
Temp : 25.9°C
QNH : 1011hPa
RH : 63%
clear sky
Wind: NNW 8kt

9th 11:00am
Temp : 34.3°C
QNH : 1010hPa
RH : 35%
clear sky
Wind: NNW 7kt

9th 2:00pm
Temp : 34.1°C
QNH : 1007hPa
RH : 38%
few clouds
Wind: NNE 8kt

9th 5:00pm
Temp : 26.1°C
QNH : 1008hPa
RH : 67%
broken clouds
Wind: NE 16kt
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), WordPress Shortcode
Description: Weather Forecasts with OpenWeatherMap in WordPress or PHP.
Download • Version 0.1, 1.9K, zip, Category: WordPress Shortcodes
PHP Code & Snippets, (1.8K)