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 couldn’t find 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 */
$tw_api_call = file_get_contents( '' . $url );
$tw_shares = json_decode( $tw_api_call );

/* Facebook */
$fb_api_call = '' . urlencode( $url );
$fb_shares = json_decode( file_get_contents( $fb_api_call ), true );

/* LinkedIn */
$li_api_call = file_get_contents( '' . $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 for the Twitter shares for

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


In this case, the information we want is "count":6. The URL 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 used to make it easy to get share counts. You will still see Twitter API calls like this one: 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.


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

Here’s what that gets you:

"id": "",
"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 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-&gt;count
  • Facebook: $fb_shares[0][share_count]
  • LinkedIn: $li_shares-&gt;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, 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!

Published by Sam Glover

Sam Glover is a lawyer and the founder and Editor in Chief of He also works with lawyers on motion practice and appeals, and is President of the board of directors of HOME Line, a nonprofit Minnesota tenant advocacy organization.

Leave a comment

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