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

A few weeks ago I posted some code that would retrieve an aviation metar from the NOAA website and display it within your WordPress blog post or page. As expected, it was followed with a few emails asking how we could output a TAF (or Terminal Aerodrome Forecast) instead. Using the same recycled code, it’s easily accomplished.

For all you non-aviation folk, a TAF is a format for reporting weather forecast information normally associated with a specific radius from an aerodrome. It is usually valid for a specified time period – usually from 3 to 24 hours. The validity of the report is included in the first line of the report.

The function is handy if you build a page that lists reports (perhaps in company with a Metar report) associated with your local airport. If you’re hosting an aviation event or airshow, it’s also nice to display weather.

There are two functions you can choose from. The first function will retrieve the TAF and simply display it on your website. As we’ve previously discussed, making repeated attempts to call the NOAA website may be deemed to be abuse (particularly if you have a busy website) so it’s better to locally cache the results for a specified time period. The second function below will do this using the WordPress Transient API.

Shortcode is the easiest way of including dynamic functionality into your blog so this is how the two functions are created for presentation within your post.

Example TAF report for Sydney (YSSY):

TAF FOR YSSY (Issued: Sat, May 18th 2013 6:33 PM UTC):TAF YSSY 181705Z 1818/1924 27013KT CAVOK FM182300 23013KT CAVOK

This is the most basic function. It will not cache results.

function get_taf($atts, $content = null) {
  extract(shortcode_atts(array(
    'loc' => 'YSSY'
  ), $atts));
$fileName = "ftp://tgftp.nws.noaa.gov/data/forecasts/taf/stations/$loc.TXT";
	$taf = '';
	$fileData = @file($fileName) or die('TAF 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)) {
			$taf .= ' ' . trim($line);
			}
		$taf = trim(str_replace('  ', ' ', $taf));
		}
	return "<blockquote>TAF FOR $loc (Issued: $time UTC):<br>$taf</blockquote>";
	}
add_shortcode('taf', 'get_taf');

The preferred function is below. It will cache the results for a time period as specified. It may be worth caching the results for the validity of the report or simply for one hour. By default, it will cache the results for 60 minutes although you can alter this by passing the parameter of time=”something” as detailed in the shortcode example below.

You will want to paste this code into your WordPress theme’s functions.php file.

function get_taf($atts, $content = null) {
  extract(shortcode_atts(array(
    'loc' => 'YSSY',
    'time' => '3600'
  ), $atts));

$transient = "$loc.taf";
$dbresult =  get_transient($transient);

if ($dbresult == true  )
	{
		return $dbresult;
	} else {

	$fileName = "ftp://tgftp.nws.noaa.gov/data/forecasts/taf/stations/$loc.TXT";
	$taf = '';
	$transient = "$loc.taf";
	$fileData = @file($fileName) or die('TAF 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)) {
			$taf .= ' ' . trim($line);
			}
		$taf = trim(str_replace('  ', ' ', $taf));
		}
	$result = "<BLOCKQUOTE>TAF FOR $loc (Issued: $time UTC):$taf</BLOCKQUOTE>";
	// 3600 will store the transient result for 1 hour
	set_transient($transient, $result, $time);
	return $result;

	}

}
add_shortcode('taf', 'get_taf');

You can output your default TAF (as specified in the function) using the shortcode of:

[taf]

If you want to generate a report of another airport (remembering that it has to be listed on the NOAA website), use the following format:

[taf loc=”YBBN”]

If you find this code useful, it would be nice to point others to the source.

Blue skies!

First Name:
Your Email Address:
 


If you liked this article, you may also like:

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

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

Comments

  1. Aaron says:

    hi there,

    while i am much appreciative of the code you have provided i do have one small problem… how can i get the short code to activate? i.e. where must i place it?

    any help you can give would be much appreciated….

    Kind Regards

    • Marty says:

      Hi Aaron. With most themes, you’ll find a functions.php file that includes most of the functions called upon by that theme. You’ll find it if you navigate (with FTP software) to, perhaps, wp-content/themes/your-theme-name. You'll probably find it in the same directory as other files such as header.php, comments.php, header.php and others. If you open up functions.php you can place the function virtually anywhere as long as it doesn't break any of the other code. To be safe, it might be an idea to paste it directly underneath the opening <?php tag at the top of the page.

      Once you have the code in the functions file, you can then include the shortcode into the HTML component of your WordPress editor.

      Does that answer your question? Let me know how you go.

  2. Dan Malcolm says:

    Thanks for providing the PHP code for retrieving TAF data. As a pilot/cfi I am interested in weather and as ham operator, I wanted to provide a weather report via digital mode transmission. Problem is most hams can’t read raw METAR/TAF. I wrote a translator for METAR and wanted to do the same for TAF and then found your code. Unfortunately it isn’t retrieving a TAF for me. The airport code I looked for is KHSV and there is TAF data for it but the path you provided doesn’t seem to work. Can you help?

  3. Marty says:

    Hi Dan. There’s a range of issues that may have prevented you from reading the TAF. Try the code below – it’ll just output the TAF as a string… and it’s exceedingly simple. If all you want to do is parse it, then it’s probably all you need. The above was geared towards formatted reports in WordPress.

    <?php
    $taf = file_get_contents('ftp://tgftp.nws.noaa.gov/data/forecasts/taf/stations/KHSV.TXT');
    print $taf;
    ?>

    Let me know if that helps, or if there’s anything else you need.

    • Dan Malcolm says:

      Hey thanks Marty. That works. Parsing is easy. and I have code that already does that for METAR. This started out as a tool for amateur radio digital mode transmission and has grown to where I now want to post the results on my aviation weather page. It’s been fun, and I thank you for the help.

Speak Your Mind

*