RBA Cash Rate: 4.35% · 1AUD = 0.67 USD · Inflation: 4.1%  
Leading Digital Marketing Experts | 1300 235 433 | Aggregation Enquires Welcome | Book Appointment
Example Interest Rates: Home Loan Variable: 5.20% (5.24%*) • Home Loan Fixed: 5.48% (6.24%*) • Fixed: 5.48% (6.24%*) • Variable: 5.20% (5.24%*) • Investment IO: 5.78% (6.81%*) • Investment PI: 5.49% (6.32%*)

Count the Number of Pinterest URL Shares with WordPress Shortcode (or PHP)

Count the Number of Pinterest URL Shares with WordPress Shortcode (or PHP)

While we've shied away from Pinterest in the past, we're quickly catching up on lost time with a series of posts detailing various ways in which to interact with the platform. This article will show you how to include a Pinterest URL count with WordPress shortcode. If you're not using WordPress there's a naked PHP function that follows.

Note: This snippet is undergoing a rewrite. Provided code will fail. Check back soon.

The Result

We'll use a page from realsimple.com for our example because they always seem to attract a large number of pins. To render the count for a URL, use [pins url="http://www.realsimple.com/holidays-entertaining/holidays/christmas/how-to-hang-christmas-lights"]. The result: N/A.

The 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
 Count the Number of Pinterest URL Shares with WordPress Shortcode (or PHP)
4
 http://www.beliefmedia.com/count-pinterest-pins
5
*/
6
 
7
8
 
9
  $atts = shortcode_atts(array(
10
    'url' => '',
11
    'n' => 1, /* Number format 1 or 0 */
12
    'cache' => 3600 * 13
13
  ), $atts);
14
 
15
  $transient = 'pinurl_' . md5(serialize($atts));
16
  $cached =  get_transient($transient);
17
 
18
  if ($cached !== false ) {
19
    return $cached;
20
 
21
  } else {
22
 
23
     if ($atts['url'] != '') {
24
 
25
       $api = @file_get_contents('http://api.pinterest.com/v1/urls/count.json?callback%20&url=' . $atts['url']);
26
       if ($api !== false) $body = preg_replace( '/^receiveCount\((.*)\)$/', '\\1', $api );
27
         else return 'Unavailable';
28
 
29
       /* Decode JSON */
30
       $count = json_decode($body);
31
 
32
       /* Result */
33
       $return = (!is_numeric($count->count)) ? '0' : $count->count;
34
       if ($atts['n']) $return = number_format($return);
35
 
36
       set_transient($transient, $return, $atts['cache']);
37
       return $return;
38
 
39
       } else {
40
 
41
       return 'No URL';
42
 
43
    }
44
  }
45
}
46
add_shortcode('pins', 'beliefmedia_count_url_pinterest_pins');

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

url

The URL is the page you're requesting a count from.

n

By default we'll return the number in a number format . To silence the number format feature, use n="0" or hardcode it into the function.

cache

The cache is the number of seconds we'll store the Pinterest count locally. Be default we'll store it for 12 hours (3600 x12) before making another request to the Pinterest API.

The PHP Function

Used outside of WordPress, the following function can be used. Usage requires Simple Cache to avoid repeated requests to Pinterest.

1
<?php 
2
include('../simple-cache/cache.inc.php');
3
 
4
/*
5
 Count the Number of Pinterest URL Shares with WordPress Shortcode (or PHP)
6
 http://www.beliefmedia.com/count-pinterest-pins
7
*/
8
 
9
function beliefmedia_count_url_pinterest_pins($url, $n = true, $cache = '43200') {
10
 
11
  $transient = 'pinterest_count_url_' . md5(serialize(func_get_args()));
12
  $cached =  beliefmedia_get_transient($transient, $cache);
13
 
14
  if ($cached !== false ) {
15
    return $cached;
16
 
17
  } else {
18
 
19
     if ($url != '') {
20
 
21
       $api = @file_get_contents( 'http://api.pinterest.com/v1/urls/count.json?callback%20&url=' . $url );
22
       if ($api !== false) $body = preg_replace( '/^receiveCount\((.*)\)$/', '\\1', $api );
23
         else return 'Unavailable';
24
 
25
       /* Decode JSON */
26
       $count = json_decode($body);
27
 
28
       /* Result */
29
       $return = (!is_numeric($count->count)) ? '0' : $count->count;
30
       if ($atts['n'] !== false) $return = number_format($return);
31
 
32
       beliefmedia_set_transient($transient, $return);
33
       return $return;
34
 
35
       } else {
36
 
37
       return 'No URL';
38
 
39
    }
40
  }
41
}
42
 
43
/* Usage */
44
$url = 'http://www.realsimple.com/holidays-entertaining/holidays/christmas/how-to-hang-christmas-lights';
45

Considerations

  • The JSON response doesn't come from a Pinterest API designed to function as described above. Continued availability can't be relied upon.

Download

Available soon.

Download our 650-page guide on Finance Marketing. We'll show you exactly how we generate Billions in volume for our clients.

  E. Australia Standard Time [ UTC+10, Default ] [ CHECK TO CHANGE ]

  Want to have a chat?
 

RELATED READING

Like this article?

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

Leave a comment