4 minutes

How to Get Social Share Counts in WordPress

12 Comments

You will be hard-pressed to find someone that doesn’t belong to a social media network these days.

Virtually all social networks provide buttons that simplify the process of sharing website content to their platforms.

To take advantage of the large user base and high engagement of social networks and bookmarking sites (such as Facebook, Twitter, Google+ and StumbleUpon), webmasters and site administration often add social buttons to their sites.

In this article, we are not going to build another social sharing plugin; rather we will learn how to programmatically get the number of social counts from Facebook, Twitter, Google Plus, Pinterest etc. onto your site.

Armed with this knowledge; in the second part of this series, we will build a WordPress plugin that displays share, likes and tweet count as text in WordPress in the second part of this article.

Note: There are a lot of social network and bookmarking websites. For the sake of brevity (and impact on the most users), we will focus on the following: Facebook, Twitter, Google+, LinkedIn, StumbleUpon and Pinterest.

Why Bother With Social Count?

I’m certain this is a question in someone’s mind right now. With a social count, you can make your site look cool when you add a social profile and share buttons with real-time count of social engagement of a content or blog post.

Below is a screenshot of mashable.com sharing buttons along with the total share count.

Impress your audience with animated websites and web presentations.

With Slides, we don’t make you start from an empty slate. All you have to do is to pick the elements you like best and combine them. Each slide has been carefully crafted to satisfy three key criteria: aesthetic, function and usability. That way you know every element works together seamlessly while enhancing the impact of your content.

Create a Website

Mashable share buttons with total share count

Another example is The Next Web similar to Mashable in that it also display the total share count.

The Next Web Social Buttons

Getting Social Network Share Count

In no particular order, let’s see how we can programmatically get the social share count of a webpage.

Facebook Likes and Shares

To get the number of times a webpage has been liked and shared on Facebook, pass the page URL to Facebook Graph API http://graph.facebook.com/?id=.

Say we want to get the number of Likes and Shares this URL https://designmodo.com/wordpress-https/, passing the URL to the Graph API as follows http://graph.facebook.com/?id=https://designmodo.com/wordpress-https/ will output the following JSON information with the count at property shares.

{
   "id": "https://designmodo.com/wordpress-https/",
   "shares": 140
}

To get the Facebook likes and shares count of any web page in PHP, pass the URL to the PHP function below:

$facebook_like_share_count = function ( $url ) {

	$api = file_get_contents( 'http://graph.facebook.com/?id=' . $url );

	$count = json_decode( $api );

	return $count->shares;
};

The code below uses the function $facebook_like_share_count to output the likes and share count of https://designmodo.com/wordpress-https/.

echo $facebook_like_share_count( 'https://designmodo.com/wordpress-https/' );

Bonus: Getting the like count of a Facebook page is quite similar to that of a webpage. The difference is while the former is in property likes, the latter is in shares.

The code below will output the number likes the official Designmodo Facebook Page has gotten.

$facebook_page_like_count = function ( $url ) {

	$api = file_get_contents( 'http://graph.facebook.com/?id=' . $url );

	$count = json_decode( $api );

	return $count->likes;
};

echo $facebook_page_like_count( 'http://www.facebook.com/designmodo' );

Twitter Posts

To get the number of times a post, content or web page got tweeted, make an API call to Twitter as follows: https://cdn.api.twitter.com/1/urls/count.json?url=http://web-page-url.com where http://web-page-url.com denote the page URL.

{
    "count":276,
    "url":"https://designmodo.com/wordpress-https/"
}

Similar to the function code for Facebook likes and share count, the code below output the tweets count of the page.

$twitter_tweet_count = function ( $url ) {

	$api = file_get_contents( 'https://cdn.api.twitter.com/1/urls/count.json?url=' . $url );

	$count = json_decode( $api );

	return $count->count;
};

echo $twitter_tweet_count( 'https://designmodo.com/wordpress-https/' );

Pinterest Pins

Pinterest is a social network that allows users to visually share, and discover new interests by posting (known as ‘pinning’ on Pinterest) images, videos and article or page bookmarks to their own or others’ boards (i.e. a collection of ‘pins,’ usually with a common theme) and browsing what other users have pinned.

To get the number of times a given URL have been shared to Pinterest, use the function code below:

$pinterest_pins = function ( $url ) {

	$api = file_get_contents( 'http://api.pinterest.com/v1/urls/count.json?callback%20&url=' . $url );

	$body = preg_replace( '/^receiveCount\((.*)\)$/', '\\1', $api );

	$count = json_decode( $body );

	return $count->count;

};

echo $pinterest_pins( 'https://designmodo.com/' );

P.S. the output of the Pinterest API is not fully JSON because the JSON is encapsulated in a bracket prefixed with the text receiveCount. I had to eliminate the redundant bracket and text to make it a full JSON.

LinkedIn Share

To get count of LinkedIn shares a post have gotten, make an API call to https://www.linkedin.com/countserv/count/share?url=http://xyz.com&format=json.

 {
        "count":405,
        "fCnt":"405",
        "fCntPlusOne":"406",
        "url":"http:\/\/designmodo.com"
    }

To get the LinkedIn count via PHP, use the function below.

$linkedin_share = function ( $url ) {

	$api = file_get_contents( 'https://www.linkedin.com/countserv/count/share?url=' . $url . '&format=json' );

	$count = json_decode( $api );

	return $count->count;

};

echo $linkedin_share( 'https://designmodo.com/' );

StumbleUpon Stumbles

To retrieve the number of times visitors stumbled on your post or web page, use the code as written below.

$stumbleupon = function ( $url ) {

	$api = file_get_contents( 'https://www.stumbleupon.com/services/1.01/badge.getinfo?url=' . $url );

	$count = json_decode( $api );

	return $count->result->views;

};

echo $stumbleupon ( 'https://designmodo.com/' );

Google PlusOnes

All the API call made so far have all been GET request. The Google+ API require a POST request.
We won’t be using the file_get_contents() to send request to the API as it can’t send POST requests hence cURL will be used.

The function below query the Google+ API and returns the +1 count of a URL.

$google_plusones = function ( $url ) {
	$curl = curl_init();
	curl_setopt( $curl, CURLOPT_URL, "https://clients6.google.com/rpc" );
	curl_setopt( $curl, CURLOPT_POST, 1 );
	curl_setopt( $curl, CURLOPT_POSTFIELDS, '[{"method":"pos.plusones.get","id":"p","params":{"nolog":true,"id":"' . $url . '","source":"widget","userId":"@viewer","groupId":"@self"},"jsonrpc":"2.0","key":"p","apiVersion":"v1"}]' );
	curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true );
	curl_setopt( $curl, CURLOPT_HTTPHEADER, array( 'Content-type: application/json' ) );
	$curl_results = curl_exec( $curl );
	curl_close( $curl );
	$json = json_decode( $curl_results, true );

	return intval( $json[0]['result']['metadata']['globalCounts']['count'] );
};

echo $google_plusones('https://designmodo.com/wordpress-https');

Summary

So far, we’ve learned how to programmatically retrieve the share count of various social media networks from their respective APIs using PHP.

As previously mentioned in the introduction, a WordPress plugin that displays the share count of various social networks in a post using shortcodes is coming next in the second part of this series.

Until then, Happy Coding.

Agbonghama Collins

Agbonghama Collins is a web developer by day and a freelance writer / blogger by night. When not wrangling with code, he is fond of sleeping on the couch and writing on his personal blog. Follow me on Google+ and Twitter

Posts by Agbonghama Collins
Cookie Icon
We use cookies to ensure that we give you the best experience on our website.
  • I disagree
  • I agree