Easily post an aviation Metar report into your WordPress post or page with shortcode

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: Thu, July 24th 2014 7:30 PM UTC):YSSY 241930Z 33006KT CAVOK 10/08 Q1020 NOSIG

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:
  1. Easily post an aviation TAF report into your WordPress post or page with shortcode
  2. Output METAR Data on your Webpage with PHP
  3. WordPress Shortcode for FMA Annunciations, FMC Messages & the MCP
  4. Metar to Twitter – Free service
  5. This Day in Aviation History on Your Website
  6. Add Episodes of Flight Podcast to your website or blog
  7. Top Of Climb Checks

About Marty

Marty is an International airline pilot, commercial helicopter pilot and experienced flight instructor. He is also the Director of a media company based in Sydney, Australia. Connect with Marty on Twitter, Flight Podcast or Google+.

Trackbacks

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

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

  3. [...] to source and manipulate data. We’ve previously provided shortcode that would render a TAF or METAR in your post. Shortcode can be tricky if you’re not overly tech savvy, so if you require [...]

Please leave a comment or question

*