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

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