How to Get Social Share Counts in WordPress

• 3 minutes READ

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.

Online Email Template Builder

With Postcards you can create and edit email templates online without any coding skills! Includes more than 100 components to help you create custom emails templates faster than ever before.

Try FreeOther Products

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 sharing buttons along with the total share count.

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 .

Say we want to get the number of Likes and Shares this URL, passing the URL to the Graph API as follows will output the following JSON information with the count at property shares.

Create Websites with Our Online Builders

With Startup App and Slides App you can build unlimited websites using the online website editor which includes ready-made designed and coded elements, templates and themes.

Try Startup App Try Slides AppOther Products
   "id": "",
   "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( '' . $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

echo $facebook_like_share_count( '' );

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( '' . $url );

	$count = json_decode( $api );

	return $count->likes;

echo $facebook_page_like_count( '' );

Twitter Posts

To get the number of times a post, content or web page got tweeted, make an API call to Twitter as follows: where denote the page URL.


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( '' . $url );

	$count = json_decode( $api );

	return $count->count;

echo $twitter_tweet_count( '' );

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( '' . $url );

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

	$count = json_decode( $body );

	return $count->count;


echo $pinterest_pins( '' );

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 .


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

$linkedin_share = function ( $url ) {

	$api = file_get_contents( '' . $url . '&format=json' );

	$count = json_decode( $api );

	return $count->count;


echo $linkedin_share( '' );

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( '' . $url );

	$count = json_decode( $api );

	return $count->result->views;


echo $stumbleupon ( '' );

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, "" );
	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('');


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.

Posts by Agbonghama Collins
Earn 25% commission on affiliate sales

We build high-quality products

Designmodo offers advanced drag and drop website and email builders for web designers and developers, we have everything you need to make money.

Cookie Icon

We use cookies to ensure that we give you the best experience on our website. Privacy Statement.

  • I disagree
  • I agree