pinterest-count-api-wordpress-shortcode

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

We’ve provided a large number of WordPress shortcodes and PHP functions in the past that will count shares on various social networks. 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,224.

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.

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.

You’ll want to cache the returned data locally in a text file or database to avoid making repeated requests to the API.

Download the Code

Download the WordPress function here and the PHP function here .

Shortt URL for this post:

Leave a Reply

Your email address will not be published. Required fields are marked *