I published some PHP code about 12 months ago that would output a raw aviation metar onto your website. This code is a variation on a theme and will post a Metar directly into a WordPress post or page with the use of shortcode. It’s simple.
I’ve just started to update a blog that I haven’t given much consideration to over the last 6 months. Internoetics.com is a site I use as a bit of a brain-dump to compile a bunch of tips, tricks, code and marketing techniques that I employ to manage several hundred websites as a one-man show. This code is in response to a couple of requests I had via that site. I’m not sure exactly why one would use it… but it’s still pretty funky.
The output of the below function will look like this (in my case, a METAR report for Sydney).
METAR FOR YSSY (Issued: Wed, June 19th 2013 1:00 AM UTC):YSSY 190100Z 21019KT 9999 VCSH FEW020 SCT030 14/08 Q1022 FM0100 MOD TURB BLW 5000FT
If you have lots of traffic, the function will make repeated attempts to call the NOAA website, so you’re probably best using a method to cache the results for the validity of the report. I’ve provided an example below.
As always, copy the below function into your theme’s functions.php file (or, if you’re using Thesis, into your custom_functions.php file).
function get_metar($atts, $content = null) {
extract(shortcode_atts(array(
'loc' => 'YSSY'
), $atts));
$fileName = "http://weather.noaa.gov/pub/data/observations/metar/stations/$loc.TXT";
$metar = '';
$fileData = @file($fileName) or die('METAR not available');
if ($fileData != false) {
list($i, $date) = each($fileData);
$utc = strtotime(trim($date));
$time = date("D, F jS Y g:i A",$utc);
while (list($i, $line) = each($fileData)) {
$metar .= ' ' . trim($line);
}
$metar = trim(str_replace(' ', ' ', $metar));
}
return "<blockquote>METAR FOR $loc (Issued: $time UTC):<br>$metar</blockquote>";
}
add_shortcode('metar', 'get_metar');
You can output a default METAR (as specified in line 3) using the shortcode of:
[metar]
If you want to generate a report of another airport, use the following format:
[metar loc=”YBBN”]
Note that the ICAO airport code is in UPPER-case and it must exist on the NOAA website.
As stated, repeated attempts to call the NOAA website may get your IP address banned if it’s demmed to be causing a high load. In any case, it’s better practice to call a locally cached result from your database that will be valid for the duration of the data you’re storing.
Here is another example; this time using WordPress transient functionality to store your result locally for a defined period of time.
function get_metar($atts, $content = null) {
extract(shortcode_atts(array(
'loc' => 'YSSY'
), $atts));
$transient = "$loc.metar";
$dbresult = get_transient($transient);
if ($dbresult == true )
{
return $dbresult;
} else {
$fileName = "http://weather.noaa.gov/pub/data/observations/metar/stations/$loc.TXT";
$metar = '';
$transient = "$loc.metar";
$fileData = @file($fileName) or die('METAR not available');
if ($fileData != false) {
list($i, $date) = each($fileData);
$utc = strtotime(trim($date));
$time = date("D, F jS Y g:i A",$utc);
while (list($i, $line) = each($fileData)) {
$metar .= ' ' . trim($line);
}
$metar = trim(str_replace(' ', ' ', $metar));
}
$result = "METAR FOR $loc (Issued: $time UTC):
$metar
";
// 3600 will store the transient result for 1 hour
set_transient($transient, $result, 3600);
return $result;
}
}
add_shortcode('metar', 'get_metar');
If you find this useful, please post a comment and let me know. If you use it on your site, a link to this post or blog would be appreciated.
Related posts:
- Easily post an aviation TAF report into your WordPress post or page with shortcode
- Output METAR Data on your Webpage with PHP
- WordPress Shortcode for FMA Annunciations, FMC Messages & the MCP
- Metar to Twitter – Free service
- Add Episodes of Flight Podcast to your website or blog
- Help us support the Centennial of Woman Pilots
- Aviation Ringtones for your mobile!



[...] few weeks ago I posted some code that would retrieve an aviation metar from the NOAA website and display it within your WordPress [...]