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 Insult on Your Website with PHP or WordPress Shortcode

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

Insults has to be one of the most pointless plugins in the WordPress repository. It was the first plugin we ever submitted to WP and was done so - like a few others - just to test the WordPress waters and get a feel for how their approval process worked. The plugin will retrieve an 'insult' from a database of thousands and return it. Simple. Like all plugins, it'll find its place somewhere.

This post will show you how to access a random insult via our API with shortcode, PHP, or with our WordPress plugin.

The Results

The result of this function will render something like this on your website:

"I hear that when your mother first saw you, she decided to leave you on the front steps of a police station while she turned herself in."

Cheesy, we know.

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.

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.

1
<?php 
2
/*
3
 Display a Random Insult on Your Website with PHP or WordPress Shortcode
4
 http://www.beliefmedia.com/display-random-insult
5
*/
6
 
7
function beliefmedia_insult($atts) {
8
  $atts = shortcode_atts(array(
9
    'tags' => false,
10
    'p' => false,
11
    'temp' => 300, /* If API offline, number of seconds to wait before trying again */
12
    'offline' => '<a href=&quot;http://www.beliefmedia/&quot;>API</a> Offline. Try again in a few minutes.',
13
    'cache' => 3600
14
  ), $atts);
15
 
16
 $transient = 'bmis_' . md5(serialize($atts));
17
 $cachedposts = get_transient($transient);
18
 if ($cachedposts !== false) {
19
 return $cachedposts;
20
 
21
 } else {
22
 
23
 /* Tags? */
24
 if ($atts['tags'] !== false) {
25
  $tags = explode(',', $atts['tags']);
26
    foreach($tags as $tag) {
27
      $htmltag .= '<' . $tag . '>';
28
    }
29
  $html_tags_closing = str_replace('<', '</', $htmltag);
30
 }
31
 
32
  /* Get data from BeliefMedia API */
33
  $json = @file_get_contents('http://api.beliefmedia.com/insults/random.php');
34
  if ($json !== false) $data = json_decode($json, true);
35
 
36
   if ($data['status'] == '200') {
37
 
38
     /* Get insult */
39
     $return = (string) $data['data']['insult'];
40
     if ($atts['tags'] !== false) $return = $htmltag . $return . $html_tags_closing;
41
     if ($atts['p'] !== false) $return = '<p>' . $return . '</p>';
42
 
43
     /* Set transient */
44
     set_transient($transient, $return, $atts['cache']);
45
 
46
     } else {
47
 
48
     $return = $atts['offline'];
49
     set_transient($transient, $return, $expiration = $atts['temp']);
50
   }
51
  }
52
 
53
 return $return;
54
}
55
add_shortcode('insult','beliefmedia_insult');

Usage

Insert the shortcode of [insult] wherever it is you want your insult to be rendered. The default code will cache the text for 1 hours (3600 seconds) but you can get them as often as every 6 minutes. We cache the text locally on our end for 5 minutes.

PHP Function

Used outside of WordPress, the following PHP function may be used. It requires the use of Simple Cache.

1
<?php 
2
/*
3
 Display a Random Insult on Your Website with PHP
4
 http://www.beliefmedia.com/display-random-insult
5
 http://wordpress.org/plugins/Insults/
6
 REQUIRES: http://www.beliefmedia.com/simple-php-cache (Simple Cache)
7
 include('simple-cache/cache.inc.php');
8
*/
9
 
10
function beliefmedia_random_insult($cache = '3600') {
11
 
12
 $transient = 'bminsult_' . 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/insults/random.php');
20
   if ($data !== false) $data = json_decode($data, true);
21
 
22
   if ($data['status'] == '200') {
23
 
24
     $return = (string) $data['data']['insult'];
25
 
26
     /* Set transient */
27
     beliefmedia_set_transient($transient, $return);
28
 
29
   } else {
30
     $return = beliefmedia_get_transient_data($transient);
31
   }
32
  }
33
 
34
 return $return;
35
}
36
 
37
/* Usage */
38
echo beliefmedia_random_insult();

Download

The plugin version is hosted in the WordPress plugin repository.


Title: Display a Random Insult (Shortcode)
Description: Display a Random Insult on Your Website with PHP or WordPress Shortcode.
  Download • Version 0.2, 923.0B, zip, Category: WordPress Shortcodes
PHP Code & Snippets, (664.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