(Fairly) Simple Shared Count

It may not be worth much toward achieving the goals for your site, but on the other hand a little bragging never hurt!

Okay, so a lot of bragging can hurt – that’s why we’re keeping this simple and subtle. But feel free to include this at the top of every page on your site, as large as it will fit above the fold, in bright pink. Do it.

Mashable has probably the hottest sharing options and styling right now.

Mashable has probably the hottest sharing options and styling right now.

Three simple steps

While looking into adding a Mashable-style share bar to my blog, I came across a bunch of tutorials and articles that I just couldn’t use. Almost all of the tutorials were terrible and most of the articles just offered the same three WordPress plugins over and over. I’m not using WordPress so they were useless, but even if I was, I hated that I wouldn’t be able to modify the code easily.

So I kept looking and finally stumbled onto this article from Internoetics that captures exactly what I hoped to achieve. While it did teach you how to implement this into WordPress, I was able to use the code on my PHP powered, non-WordPress site.

I could have shared that article with you and said, “have at it,” but I’ve made some improvements and hopefully will walk you through the process a bit better. The code from that article utilizes api.sharedcount.com which is now deprecated and slated to be shut down. So I’ve updated the code to be future-friendly and use an API key which is now required.

Here are the three quick and simple steps to get shared counts on your site in just a few minutes.

screenshot of SharedCount.com homepage

1. Sign up for SharedCount.com

Head over to http://sharedcount.com and sign up for a free account to get an API key that we’ll be using later.

2. Create a cache directory

In your hosting space, you’ll need to create a new folder to store our cached shared count data. We need a folder because each URL will get its own text file and we don’t want to clog up any important directories. You can pick any location that is accessible by PHP and you can name it whatever you like.

3. Place code where you want the shared count to display

You can include the following code in your PHP exactly where you want the shared count to be output. I’ve wrapped it in a function so I have a bit more flexibility. Wherever I want to output the shared count, I simply insert <?php echo sharedcount('http://example.com/current-url'); ?> with the current page’s URL as the only parameter.

Copy and paste the following code block into your PHP and make sure to customize the top three variables. $key should be set to the API key you got from SharedCount in the first step. $cache should be set to the amount of time in seconds between lookups. Remember you have limits to your free SharedCount account. Finally, for $cacheFile, change ‘cachefolder/’ to the cache directory you setup in the second step.

function sharedcount($url) {
    $key = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'; // API key from sharedcount.com
    $cache = 3600; // cache time in seconds
    $cacheFile = 'cachefolder/' . md5($url) . '.txt';

    if (file_exists($cacheFile) && (time() - $cache < filemtime($cacheFile))) {
        $result = file_get_contents("$cacheFile");
        return $result;
    } else {
        $json = file_get_contents("http://free.sharedcount.com/?url=" . rawurlencode($url) . "&apikey=" . $key);
        $counts = json_decode($json, true);
        $result = $counts['Twitter'] + $counts['Facebook']['total_count'] + $counts['GooglePlusOne'] + $counts['StumbleUpon'] + $counts['Pinterest'] + $counts['LinkedIn'];

        $fp = fopen($cacheFile, 'w');
        fwrite($fp, $result);
        return $result;

Example usage:

<div class="total-shares">
    <p>This post has been shared <?php echo sharedcount(current_url()); ?> times.</p>

As you can see, my version adds up the shares from Twitter, Facebook, Google+, StumbleUpon, Pinterest, and LinkedIn. You can add or subtract from that set to get the shares you want. See SharedCount’s documentation for your options here.

If you don’t want to use a function, you’ll need to set a variable $url to the current page’s URL first, then insert everything but function sharedcount($url) { and it’s closing }. Then change the two return $result; lines to `echo $result;’ instead. It should work like this the same way.

It’s that simple!

You can definitely flesh this out a bit more. Simplify it, clean it up, style it in bright pink! My site doesn’t yet pull much traffic or shares, so my testing of this is limited. If you’re using this on a larger site, I’d love to hear about your experience. And no matter what size your site is, if you have a comment or question, please leave that below!