How to Get Social Share Counts in WordPress


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 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.

   "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.


  1. Rafael Angeline Oct 3, 9:19 pm


    It sounds really useful, I’ll try it in a couple minutes! I hope it fits my needs hehehe

    Thank you!

  2. Sanc Oct 13, 2:09 am

    Thanks a lot for this snnipets Agbonghama! I found them so useful for a certain project I’m developing right now :)

    I wonder if you know something about the awesome graph that Mashable appends aside every social media buttons… I’ve been searching a long time how to make this, but there is nothing about it yet…

    Thanks again!

    • Sanc Oct 17, 7:16 pm

      No answer? :(

      • Agbonghama Collins Oct 17, 8:56 pm

        Sorry for the late reply.

        I currently have no idea on how the graph works.

        If I have the time, I will research it and give you a feedback.

    • Rafael Angeline Oct 17, 10:23 pm


      Actually make a graph like Mashable is a bit harder, but not so hard. A way to do this would be:

      – Get the social values using the methods above everyday or every period
      – Sum the values to get total shares/likes
      – Record it in a field of the post, writing something like 17/10:100, where first would be date and the second would be the total
      – Use a line graph JS lib to generate the lines (Mashable uses a canvas, so it is generated with JS, what avoid overload the server)

      The hardest thing here is record the totals everyday, that could be done with a WP Cron, maybe?

      I hope it helps!

      • Agbonghama Collins Oct 18, 7:13 pm

        Thank you Rafael Angeline.

        That’s pretty much how the mashable graph can be created and how it works.

  3. zoomingjapan May 25, 5:46 pm

    Hello! :)

    I’m trying to use this in WordPress.
    I’ve put the code snippets I need into my function.php and it spits out the count just fine as long as I insert a specific blog URL myself.

    However, if I want to replace that URL so that it will fetch the URL of the blog article we’re on, it won’t work. It either spits out nothing or “0”.

    I’ve tried the following:

    <?php echo $facebook_like_share_count( '' ); ?>
    <?php echo $facebook_like_share_count( '' ); ?>

    • Rhys Clay Oct 7, 10:00 am

      Hey @Zoomingjapan check out my comment below it’s exactly what you need!

  4. newbie Jul 5, 12:59 pm

    It works for a few days but now I have this error: Notice: Undefined property: stdClass::$shares in /home/my_domain/public_html/wp-content/themes/my_theme/template-parts/content-single.php on line 12 :/

  5. Rhys Clay Oct 7, 9:59 am

    Awesome tutorial and very comprehensive. For anybody who is interested in getting the share buttons to use the current page/post’s URL and not a static URL, try something like this:

    Then in your functions file update the functions to look something like this:

    //facebook share count
    function facebook_like_share_count ( $url ) {
    $api = file_get_contents( ‘’ . $url );
    $count = json_decode( $api );
    $count_shares = $count->shares;
    return $count_shares;

  6. Murti Nov 24, 1:38 am

    Twitter API changed and gives the error as follows:
    {“errors”:[{“message”:”Sorry, that page does not exist”,”code”:34}]}

    Can anyone tell what I need to replace the part below with. Here’s the code that needs to be updated with new api in my opinion:

Leave a Reply

* Minimum length: 20 characters