Get Twitter, Facebook, and LinkedIn Share Counts for WordPress

I’ve been meaning to add a share counter to posts on Lawyerist for a while now, and I finally sat down to figure it out. There are plugins for this, of course, but I wanted something fast and simple that I could place and style just the way I want it.

I found lots of information out there, but a lot of it is outdated, incomplete, or just doesn’t work. After some trial and error, I figured out a simple solution that works. Since I didn’t a copy-and-paste guide anywhere else, here it is.

The Code

First, here is the code:

/*------------------------------
Get Total Shares
------------------------------*/

$url = get_the_permalink(); // Stores the post URL as $url

// Get share counts for Twitter, Facebook, and LinkedIn

/* Twitter (via http://newsharecounts.com/) */
$tw_api_call = file_get_contents( 'http://public.newsharecounts.com/count.json?url=' . $url );
$tw_shares = json_decode( $tw_api_call );

/* Facebook */
$fb_api_call = 'http://api.facebook.com/restserver.php?format=json&method=links.getStats&urls=' . urlencode( $url );
$fb_shares = json_decode( file_get_contents( $fb_api_call ), true );

/* LinkedIn */
$li_api_call = file_get_contents( 'https://www.linkedin.com/countserv/count/share?url=' . $url . '&format=json' );
$li_shares = json_decode( $li_api_call );

// Add up the share counts
$shares = $tw_shares->count + $fb_shares[0][share_count] + $li_shares->count;
$shares_formatted = number_format( $shares );

// Output total shares
echo '<span class="share_count">' . $shares_formatted . ' Shares </span> ';

Now let’s break it down.

How it Works

For each network, we’ve got to make an API call to request the share count. Each is a little different, but the gist of what we’re doing is making an API call that gets us a JSON file that includes the share count. I hadn’t ever done an API call before figuring this out, but it turns out these are really simple. You can even just enter them into your browser.

For example, here is the API call to newsharecounts.com for the Twitter shares for samglover.net:

http://public.newsharecounts.com/count.json?url=http://samglover.net

If you copy and paste that into your browser, you’ll get something like this:

{
"url":"http://samglover.net",
"count":6,
"tracked":0,
"historic":6
}

In this case, the information we want is "count":6. The URL http://samglover.net has been shared 6 times on Twitter. All we’re doing is repeating that process for each network, adding up the numbers, and outputting them.

Twitter

Twitter used to make it easy to get share counts. You will still see Twitter API calls like this one: https://urls.api.twitter.com/1/urls/count.json?url=http://samglover.net. But if you try it, it won’t work. the Twitter API no longer supports share counts. (Which is stupid, but whatever.)

Fortunately, there are several other options for getting Twitter share counts. I went with NewShareCounts, which makes it easy. Just sign in with Twitter and it will start counting Twitter shares for your website. Then you can just replace the Twitter API URL with the NewShareCount API URL as I’ve done in my code.

Facebook

With Facebook, initially I tried a simple Open Graph API call:

https://graph.facebook.com/?id=https://lawyerist.com/77964/lawyers-expensive-ill-tell/

Here’s what that gets you:

{
   "id": "https://lawyerist.com/77964/lawyers-expensive-ill-tell/",
   "shares": 28152
}

But there are two problems with that. First, when you try to do that from your WordPress theme, you’ll get an error. Second, that number includes likes, comments, and shares. We just want shares.

I found another way to get a Facebook share count that (a) works when called from a WordPress theme, and (b) only includes actual shares, not likes and comments.

LinkedIn

LinkedIn just gives you the share count without any drama. Way to go, LinkedIn!

Addition and Output

Just so you know what is going on in case you want to change things, these are the bits that get the actual share count numbers:

  • Twitter: $tw_shares->count
  • Facebook: $fb_shares[0][share_count]
  • LinkedIn: $li_shares->count

I’ve added them all up into a single variable, $shares. That is the raw number. Then I format it using the number_format() function to get a formatted number, stored as $shares_formatted, with commas as thousands separators so it looks nice on the page.

Adding Total Share Count to Your WordPress Theme

The code will need to go within the Loop. If you want to see how I’ve implemented it on Lawyerist.com, you can find the code on Github. These links will take you to the relevant sections of the template files:

  • functions.php (I count shares within the lawyerist_get_postmeta() function.)
  • style.css (Searching for .postmeta and .share_count should get you most of the relevant bits.)

You’ll notice share counts are just one piece of a larger function, lawyerist_get_postmeta(), which figures out what to show beneath the title based on the kind of page, whether there is a sponsor or not, etc. I also hide share and comment counts until they get above 10. If you want a neat little share icon, you’ll need to add Genericons to your theme, and then the .share_count:before CSS should do the trick.

Hope this helps!

Leave a Reply

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