[Shortcode] Easily post an aviation Metar report into your post or page with shortcode

I published some PHP code about 12 months ago that would output a raw aviation metar onto your website. Since I’ve published a ton of shortcode lately, I’ve had two people email me and ask how a Metar report could be posted into a WordPress post or page. Once again, easily accomplished!

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).

The output of the below function will look like this (in my case, a METAR report for Sydney).

METAR FOR YSSY (Issued: Sun, May 20th 2012 5:00 AM UTC):
YSSY 200500Z 20009KT 9999 VCSH FEW010 BKN020 BKN040 15/13 Q1024 FM0600 18012KT 9999 -SHRA SCT020 SCT040

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 your default METAR (as specified in the function on 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.

First Name:
Your Email Address:
 


If you liked this article, you may also like:

  1. [Shortcode] Easily post an aviation TAF report into your WordPress post or page with shortcode
  2. Aviation METAR with PHP
  3. [Shortcode] Include an RSS feed in a post or page with shortcode
  4. Metar to Twitter Application
  5. WordPress Shortcode for FMA Annunciations, FMC Messages & the MCP (Aviation)
About Marty

is a passionate web developer from Sydney, Australia. He owns about 400 websites and makes a healthy living from working the web. As a day job, he works as a pilot for an international airline. You can follow Marty on Twitter or Google+.

Comments

  1. Sharon and Eric says:

    Brilliant work, Marty. It works great and adds a new dimension to my static WP blog.

  2. Jack Allison says:

    I have been looking all over for a METAR fetcher for my website. Thanks a lot for this.

    • Marty says:

      No worries! If you need any help with whatever you’re doing, please let me know. You may also be interested in a Metar to Twitter application I’ve got at WeatherAviation.net; I’ve got a ‘general’ post on this website scheduled in a couple of days.

Trackbacks

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

Speak Your Mind

*