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

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:

"Any friend of yours -- is a friend of yours."

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
 
8
function beliefmedia_insult($atts) {
9
  $atts = shortcode_atts(array(
10
    'tags' => false,
11
    'p' => false,
12
    'temp' => 300, /* If API offline, number of seconds to wait before trying again */
13
    'offline' => '<a href=&quot;http://www.beliefmedia/&quot;>API</a> Offline. Try again in a few minutes.',
14
    'cache' => 3600
15
  ), $atts);
16
 
17
 $transient = 'bmis_' . md5(serialize($atts));
18
 $cachedposts = get_transient($transient);
19
 if ($cachedposts !== false) {
20
 return $cachedposts;
21
 
22
 } else {
23
 
24
 /* Tags? */
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 data from BeliefMedia API */
34
  $json = @file_get_contents('http://api.beliefmedia.com/insults/random.php');
35
  if ($json !== false) $data = json_decode($json, true);
36
 
37
   if ($data['status'] == '200') {
38
 
39
     /* Get insult */
40
     $return = (string) $data['data']['insult'];
41
     if ($atts['tags'] !== false) $return = $htmltag . $return . $html_tags_closing;
42
     if ($atts['p'] !== false) $return = '<p>' . $return . '</p>';
43
 
44
     /* Set transient */
45
     set_transient($transient, $return, $atts['cache']);
46
 
47
     } else {
48
 
49
     $return = $atts['offline'];
50
     set_transient($transient, $return, $expiration = $atts['temp']);
51
   }
52
  }
53
 
54
 return $return;
55
}
56
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
    <blockquote class="wp-embedded-content" data-secret="tBqpmzXM7f"><a href="https://wordpress.org/plugins/insults/">INSULTS</a></blockquote><iframe class="wp-embedded-content" sandbox="allow-scripts" security="restricted" style="position: absolute; clip: rect(1px, 1px, 1px, 1px);" title="“INSULTS” — Plugin Directory" src="https://wordpress.org/plugins/insults/embed/#?secret=tBqpmzXM7f" data-secret="tBqpmzXM7f" width="600" height="338" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe>
6
    REQUIRES: http://www.beliefmedia.com/simple-php-cache (Simple Cache)
7
    include('simple-cache/cache.inc.php');
8
*/
9
 
10
 
11
function beliefmedia_random_insult($cache = '3600') {
12
 
13
 $transient = 'bminsult_' . md5(serialize(func_get_args()));
14
 $cachedposts = beliefmedia_get_transient($transient, $cache);
15
 if ($cachedposts !== false) {
16
 return $cachedposts;
17
 
18
  } else {
19
 
20
   $data = @file_get_contents('http://api.beliefmedia.com/insults/random.php');
21
   if ($data !== false) $data = json_decode($data, true);
22
 
23
   if ($data['status'] == '200') {
24
 
25
     $return = (string) $data['data']['insult'];
26
 
27
     /* Set transient */
28
     beliefmedia_set_transient($transient, $return);
29
 
30
   } else {
31
     $return = beliefmedia_get_transient_data($transient);
32
   }
33
  }
34
 
35
 return $return;
36
}
37
 
38
 
39
/* Usage */
40
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

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