Australia's Leading Digital Marketing Experts. T. 1300 235 433  |  Aggreagtion Enquires Welcome

Display Aviation Quotes on Your Website With WordPress or PHP

In the continued spirit of sharing ordinary plugins to the WordPress repository, we posted Aviation Quotes a few years ago. It's purpose is to simply return a random aviation quote on your website. We have a growing database of quotes and the plugin was designed primarily to service a few of our own aviation websites. It's one of a few plugins we submitted only because we used them ourselves... and WordPress provided an easy means to maintain and update them.

One of the most limiting aspects of the current setup is that we'll indiscriminately deliver a quote - comedic, serious, or otherwise. The ability to specifically request quotes of a specific nature hasn't yet been implemented. The same applies for quotes that might be considered NSFW.

While there's a WordPress plugin hosted in the WP repository, we've also provided shortcode and a PHP function.

The Result

The shortcode of [aviationquote p="1"] in your text editor will render the following on your WordPress website (blockquote is my own formatting):

“If you don't gear up your brain before takeoff, you'll probably gear up your airplane on landing.”

 

The p=1 is used to wrap the quote in paragraph <p> tags. If not used the quote will display inline (as follows): “If you don't gear up your brain before takeoff, you'll probably gear up your airplane on landing.”

Display Aviation Quotes on Your Website With WordPress or PHP

WordPress shortcodes are easy to use. Even pilots can use them.

The individual receiving attribution for the quote can be formatted with standard HTML tags (if attribution exists). For example, to render the attribution text as bold and in italics, use the following: [aviationquote p="1" tags="em,strong"]. Note that the opening and closing tags aren't used, and each html formatting instruction is separated by a comma.

"The important achievement of Apollo was demonstrating that humanity is not forever chained to this planet and our visions go rather further than that and our opportunities are unlimited." - Neil Armstrong

We use an m-dash between the quote and attribution by default. You can alter this as follows: [aviationquote p="1" sep="~"]. The result:

"Nobody who gets too damned relaxed builds up much flying time." ~ Ernest K. Gann, advice from the 'old pelican', The Black Watch, 1989

Again, the attribution will only be formatted if it is returned in the API response.

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. You may optionally download and install our plugin from the bottom of of the page.

1
<?php 
2
/*
3
    Display Aviation Quotes on Your Website With WordPress or PHP
4
    http://www.beliefmedia.com/aviation-quotes
5
*/
6
 
7
function beliefmedia_aviationquote($atts) {
8
  $atts = shortcode_atts(array(
9
    'sep' => '&mdash;',
10
    'tags' => false,
11
    'p' => false,
12
    'offline' => 'The Aviation Quotes <a href=&quot;http://www.beliefmedia.com/&quot;>API</a> is temporarily offline.',
13
    'cache_temp' => 360,
14
    'cache' => 3600,
15
  ), $atts);
16
 
17
 $transient = 'bmaq_' . md5(serialize($atts));
18
 $cachedposts = get_transient($transient);
19
 if ($cachedposts !== false) {
20
 return $cachedposts;
21
 
22
 } else {
23
 
24
 /* Construct tag(s) for attribution */
25
 if ($atts['tags'] !== false) {
26
  $tags = explode(',', $atts['tags']);
27
    foreach($tags as $tag) {
28
    $htmltag .= '<' . $tag . '>';
29
   }
30
  $html_tags_closing = str_replace('<', '</', $htmltag);
31
  }
32
 
33
  /* Get quote from BeliefMedia API */
34
  $json = @file_get_contents('http://api.beliefmedia.com/aviation-quotes/random.php');
35
  if ($json !== false) $data = json_decode($json, true);
36
 
37
   if ($data['status'] == '200') {
38
 
39
     /* Get quote */
40
     $thequote = (string) $data['data']['quote'];
41
 
42
     /* Attribution? */
43
     $attribution = ($data['data']['attribution'] != '') ? $data['data']['attribution'] : false;
44
 
45
     /* Apply tags around attribution? */
46
     if ( ($atts['tags'] !== false) && ($attribution != false) ) $attribution = $htmltag . $data['data']['attribution'] . $html_tags_closing;
47
 
48
     /* Attribution */
49
     if ($attribution !== false) $attribution = '&nbsp;' . $atts['sep'] . '&nbsp;' . $attribution;
50
 
51
     /* String to return */
52
     $result = ($attribution != false) ? $thequote . $attribution : $thequote;
53
 
54
     /* Wrap in paragraph tags? */
55
     if ($atts['p'] !== false) $result = '<p>' . $result . '</p>';
56
 
57
     /* Set transient */
58
     set_transient($transient, $result, $atts['cache']);
59
 
60
     } else {
61
 
62
     $result = $atts['offline'];
63
     if ($atts['p'] !== false) $result = '<p>' . $result . '</p>';
64
     set_transient($transient, $result, $atts['cache_temp']);
65
   }
66
 
67
  }
68
 return $result;
69
}
70
add_shortcode('aviationquote','beliefmedia_aviationquote');

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.

Shortcode Attributes

The following attributes can be used.

sep

The sep is the separator between the text and the attribution. Defaults to em-dash (--).

tags

The tags applies to the attribution. Usage: tags="strong, em".

p

To wrap the text in paragraph tags, use p="1". We only built this in because of issues with WordPress that often prevents paragraph tags being applied.

offline

To display a custom message if the API is offline, use offline="your text in here".

cache_temp

If the API is offline, wait the cache_temp before making another request.

cache

The quote will be cached for the period defined by cache. We cache on our end for (usually) 20 minutes.

PHP Function

Used outside of WordPress, the following function can be used. The function requires Simple Cache.

1
<?php 
2
/*
3
    Display Aviation Quotes on Your Website With WordPress or PHP
4
    http://www.beliefmedia.com/aviation-quotes
5
*/
6
 
7
 
8
function beliefmedia_aviationquote($args = '') {
9
 
10
  $atts = array(
11
    'sep' => '&mdash;',
12
    'tags' => false,
13
    'p' => false,
14
    'offline' => 'The Aviation Quotes <a href=&quot;http://www.beliefmedia.com/&quot;>API</a> is temporarily offline.',
15
    'cache_temp' => 360,
16
    'cache' => 3600
17
  );
18
 
19
 /* Merge $args with $atts */
20
 $atts = (empty($args)) ? $atts : array_merge($atts, $args);
21
 
22
 $transient = 'bm_avquote_' . md5(serialize($atts));
23
 $cachedposts = beliefmedia_get_transient($transient, $atts['cache']);
24
 if ($cachedposts !== false) {
25
 return $cachedposts;
26
 
27
  } else {
28
 
29
   $data = @file_get_contents('http://api.beliefmedia.com/aviation-quotes/random.php');
30
   if ($data !== false) $data = json_decode($data, true);
31
 
32
   if ($data['status'] == '200') {
33
 
34
    /* Get quote */
35
     $thequote = (string) $data['data']['quote'];
36
 
37
     /* Attribution? */
38
     $attribution = ($data['data']['attribution'] != '') ? $data['data']['attribution'] : false;
39
 
40
     /* Apply tags around attribution? */
41
     if ( ($atts['tags'] !== false) && ($attribution != false) ) $attribution = $htmltag . $data['data']['attribution'] . $html_tags_closing;
42
 
43
     /* Attribution */
44
     if ($attribution !== false) $attribution = '&nbsp;' . $atts['sep'] . '&nbsp;' . $attribution;
45
 
46
     /* String to return */
47
     $result = ($attribution != false) ? $thequote . $attribution : $thequote;
48
 
49
     /* Wrap in paragraph tags? */
50
     if ($atts['p'] !== false) $result = '<p>' . $result . '</p>';
51
 
52
     /* Set transient */
53
     beliefmedia_set_transient($transient, $result);
54
 
55
   } else {
56
 
57
     /* Else we return old data */
58
     $result = beliefmedia_get_transient_data($transient);
59
 
60
   }
61
 
62
  }
63
 return $result;
64
}
65
 
66
/* Usage */
67
68
 
69
To mimic the features of the shortcode, pass an array with 'attributes'.
70
 
71
$args = array('tags' => 'strong', 'p' => 'true');
72

Considerations

  • The feature to submit new quotes from within your dashboard is now being tested by a few colleagues. Quotes are held for moderation and later make their way into the database.

Download

Title: Display Aviation Quotes on Your Website (Shortcode)
Description: Display Aviation Quotes on Your Website With WordPress or PHP.
  Download • Version 0.2, 1.0K, zip, Category: WordPress Shortcodes
PHP Code & Snippets, (989.0B)    

Plugin Title: Author:
Description:
Download (downloaded 0 times) | Plugin Page

Like this article?

Share on facebook
Share on Facebook
Share on twitter
Share on Twitter
Share on linkedin
Share on Linkdin
Share on pinterest
Share on Pinterest

Leave a comment

READY TO HAVE A CHAT? CALL US ANYTIME ON 1300 235 433