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.67%*) • Fixed: 5.39% (6.67%*) • Variable: 5.69% (5.89%*) • Investment IO: 5.69% (6.52%*) • Investment PI: 5.49% (5.98%*)

Display a Random Fact on Your Website with PHP or WordPress Shortcode

Display a Random Fact on Your Website with PHP or WordPress Shortcode

This article will show you how to display a random fact on your website using PHP or WordPress shortcode. Not unlike our article on displaying a random quote (which is actually quite decent) and another (scheduled) article on displaying a random insult, they were created simply to make use of our suite of content APIs. In an attempt to establish a presence in the WordPress repository, we submitted the Insult plugin making it, quite possibly, the crappiest plugin WordPress has ever seen. That said, it's found a limited audience and we'll continue to support it.

In migrating our services over to our new website we'll be updating all the APIs to use JSON data almost exclusively.

Most of the facts that we have on record are interesting and valid. Others are crap and we have no idea if they're true. Some of the records are submitted via a few different third-party platforms so we don't have an overly effective way of validating all the submitted data.

The Result

The result of the [fact] shortcode will render something like this (bolding and blockquote is mine):

Diet Pepsi was originally called Patio Diet Cola.

True? No idea.

The Code

Copy and paste the WordPress function into your theme's functions.php file or, if you sensibly have one installed, your custom functions plugin.

1
<?php 
2
/*
3
 Display a Random Fact on Your Website with PHP or WordPress Shortcode
4
 http://www.beliefmedia.com/random-fact
5
 Also: https://wordpress.org/plugins/Facts/
6
*/
7
 
8
function beliefmedia_random_fact($atts) {
9
  $atts = shortcode_atts(array(
10
    'cache' => '1800'
11
  ), $atts);
12
 
13
 $transient = 'bmf_' . md5(serialize($atts));
14
 $cachedposts = get_transient($transient);
15
 if ($cachedposts !== false) {
16
 return $cachedposts;
17
 
18
  } else {
19
 
20
   $json = @file_get_contents('http://api.beliefmedia.com/facts/random.php');
21
   if ($json !== false) $data = json_decode($json, true);
22
 
23
   if ($data['status'] == '200') {
24
 
25
     $return = (string) $data['data']['fact'];
26
 
27
     /* Set transient */
28
     set_transient($transient, $return, $atts['cache']);
29
 
30
   } else {
31
     $return = 'Fact unavailable for a few minutes.';
32
     set_transient($transient, $return, $expiration = '300');
33
   }
34
  }
35
 return $return;
36
}
37
add_shortcode('fact','beliefmedia_random_fact');

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.

The cache period can be defined in your shortcode as cache="3600" (one hour), or hard-coded into your shortcode function.

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 Fact on Your Website with PHP or WordPress Shortcode
4
 http://www.beliefmedia.com/random-fact
5
 Also: https://wordpress.org/plugins/Facts/
6
 REQUIRES: http://www.beliefmedia.com/simple-php-cache (Simple Cache)
7
*/
8
 
9
function beliefmedia_random_fact($cache = '3600') {
10
 
11
 $transient = 'bmf_' . md5(serialize(func_get_args()));
12
 $cachedposts = beliefmedia_get_transient($transient, $cache);
13
 if ($cachedposts !== false) {
14
 return $cachedposts;
15
 
16
  } else {
17
 
18
   $data = @file_get_contents('http://api.beliefmedia.com/facts/random.php');
19
   if ($data !== false) $data = json_decode($data, true);
20
 
21
   if ($data['status'] == '200') {
22
 
23
     $return = (string) $data['data']['fact'];
24
 
25
     /* Set transient */
26
     beliefmedia_set_transient($transient, $return);
27
 
28
   } else {
29
     $return = beliefmedia_get_transient_data($transient);
30
   }
31
  }
32
 
33
 return $return;
34
}
35
 
36
/* Usage */
37

Download


Title: Display a Random Fact on Your Website
Description: Display a Random Fact on Your Website with PHP or WordPress Shortcode.
  Download • Version 0.2, 668.0B, zip, Category: PHP Code & Snippets
WordPress Shortcodes, (675.0B)    

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

■ ■ ■

 
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