RBA Cash Rate: 4.35% · 1AUD = 0.67 USD · Inflation: 4.1%  
Leading Digital Marketing Experts</strong | 1300 235 433 | Aggregation Enquires Welcome | Book Appointment
Example Interest Rates: Home Loan Variable: 5.69% (5.89%*) • Home Loan Fixed: 5.39% (6.59%*) • Fixed: 5.39% (6.59%*) • Variable: 5.69% (5.89%*) • Investment IO: 5.69% (6.48%*) • Investment PI: 5.39% (6.59%*)

Display a Random Quote on your Website with WordPress Shortcode

Display a Random Quote on your Website with WordPress Shortcode

A few years ago we toyed around with the idea of submitting a few plugins to the WordPress repository. Most of them were very basic simply so we could wrap our heads around the way they the WP system worked. One of the first plugins we submitted was QOTZ - a really simple plugin that'd do nothing except render a random quote on your WP website. The plugin is partnered with an unmonitored website, Twitter account , and Facebook page (all displaying the quotes as images rather than text).

A lot of what we worked on a few years ago included an XML API. While we thought XML data was appropriate at the time (mainly because it was browser-readable), history has since educated us on our wayward ways. As we rebuild a number of our websites, any API will return JSON data almost exclusively - now sensibly our norm. This post will essentially reproduce what we once shared on another site: how to render a random quote in your website with shortcode (using our newer, improved, faster, and more reliable API). The plugin update will follow.

If you're a client and have an API key, check the platform for expanded usage (such as text search, branded image quotes, quotes by author, and paginated results).

The Result

The result is as advertised: a random quote. Shortcode of [qotz] will return the following (bolding and blockquote is mine):

"It is truer to say that martyrs create faith more than faith creates martyrs." — Miguel de Unamuno

The quote is cached on your end for 15 minutes. That ensures you don't retrieve the same cached quote on our end.

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 a Random Quote on your Website with WordPress Shortcode
4
 http://www.beliefmedia.com/random-quote-wordpress-php
5
 Also: https://wordpress.org/plugins/qotz/
6
 http://www.qotz.net/
7
*/
8
 
9
function beliefmedia_wp_qotz($atts) {
10
  $atts = shortcode_atts(array(
11
    'cache' => '1800'
12
  ), $atts);
13
 
14
 $transient = 'bmq_' . md5(serialize($atts));
15
 $cachedposts = get_transient($transient);
16
 if ($cachedposts !== false) {
17
 return $cachedposts;
18
 
19
  } else {
20
 
21
   $json = @file_get_contents('http://api.beliefmedia.com/quotes/random.php');
22
   if ($json !== false) $data = json_decode($json, true);
23
 
24
   if ($data['status'] == '200') {
25
 
26
     $text = (string) $data['data']['quote'];
27
     $author = (string) $data['data']['author'];
28
     $genre = (string) $data['data']['genre'];
29
 
30
     /* Format the quote to your liking */
31
     $return = '&quot;' . $text . '&quot; &mdash; ' . $author;
32
 
33
     /* Set transient */
34
     set_transient($transient, $return, $atts['cache']);
35
 
36
   } else {
37
     $return = 'Quote unavailable for a few minutes.';
38
     set_transient($transient, $return, $expiration = '300');
39
   }
40
  }
41
 return $return;
42
}
43
add_shortcode('qotz','beliefmedia_wp_qotz');

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.

PHP Function

Used outside of WordPress the following function can be used. It requires that you include Simple Cache.

1
<?php 
2
/*
3
 Display a Random Quote on your Website with WordPress Shortcode
4
 http://www.beliefmedia.com/random-quote-wordpress-php
5
 Also: https://wordpress.org/plugins/qotz/
6
 Requries Simple Cache: http://www.beliefmedia.com/simple-php-cache
7
 http://www.qotz.net/
8
*/
9
 
10
function beliefmedia_wp_qotz($cache = '3600') {
11
 
12
 $transient = 'bmq_' . md5(serialize(func_get_args()));
13
 $cachedposts = beliefmedia_get_transient($transient, $cache);
14
 if ($cachedposts !== false) {
15
 return $cachedposts;
16
 
17
  } else {
18
 
19
   $data = @file_get_contents('http://api.beliefmedia.com/quotes/random.php');
20
   if ($data !== false) $data = json_decode($data, true);
21
 
22
   if ($data['status'] == '200') {
23
 
24
     $text = (string) $data['data']['quote'];
25
     $author = (string) $data['data']['author'];
26
     $genre = (string) $data['data']['genre'];
27
 
28
     /* Format the quote to your liking */
29
     $return = '&quot;' . $text . '&quot; &mdash; ' . $author;
30
 
31
     /* Set transient */
32
     beliefmedia_set_transient($transient, $return);
33
 
34
   } else {
35
     $return = beliefmedia_get_transient_data($transient);
36
   }
37
  }
38
 return $return;
39
}
40
 
41
/* Usage */
42
echo beliefmedia_wp_qotz($cache = '1800');

Considerations

  • Most of what's quoted on the Internet is what we wanted people to say, not what they actually said. Einstein and Marilyn Monroe are probably two of the hardest hit by distorted or fake attribution. However, short quotes are often a paraphrased sentiment, giving them a certain truth.
  • The quote can obviously be formatted with HTML to your liking.
  • Clients have access to a huge database of quotes in our library that can all be manufactured into custom images. Once created, they can be sent to social in a number of ways.
  • Despite showing a small number of downloads, we will build upon this plugin to return movie quotes and other information.

Download


Title: Display a Random Quote on your Website (Shortcode)
Description: Display a Random Quote on your Website with WordPress Shortcode or PHP.
  Download • Version 0.2, 759.0B, zip, Category: WordPress Shortcodes
PHP Code & Snippets, (738.0B)    

■ ■ ■

 
Download our complimentary 650-page guide on marketing for mortgage brokers. We'll show you exactly how we generate billions in volume for our clients.
Finance Guide, Cropped Top and Bottom
  Timezone: 1 · [ CHANGE ]

RELATED READING

Like this article?

Share on Facebook
Share on Twitter
Share on Linkdin
Share on Pinterest

Leave a comment