share-button-internoetics-countshares

Count the Number of Shares to Facebook, Twitter, LinkedIn & Google Plus

I’ve recently consolidated two websites into one as a means of managing my time and resources more effectively. The other sites will be revived in one way or another but they’ll be built ‘low maintenance’. Apart from the multiple site to manage, there was a large overlap in content that was far too difficult to ignore.

Related post: “Display Total Number of Social Shares with PHP and WordPress Shortcode”.

In the process of moving posts across I couldn’t work out an effective manner in which to retain Facebook shares, Twitter counts and so on. Instead of implementing a solution that would have been rather server intensive, I decided to simply retrieve the post count from a few networks and display that (aggregated) information under each post.

This post details how to retrieve post counts from the big four social networks and – if you’re interested enough - how to render that data on your screen in a small text box. I’ve posted about Twitter counts before in isolation; this post expands upon the same theme.

The Naked Functions

Get Number of Tweets from Twitter

Usage: echo get_tweets("http://www.internoetics.com/2012/05/16/count-shares-to-facebook-twitter-linkedin-googleplus/");

Get Number of LinkedIn Shares

Usage: echo internoetics_get_shares("http://www.internoetics.com/2012/05/16/count-shares-to-facebook-twitter-linkedin-googleplus/");

Get Number of Facebook Shares

Usage: echo internoetics_get_likes("http://www.internoetics.com/2012/05/16/count-shares-to-facebook-twitter-linkedin-googleplus/");

Get Number of Google+1’s

Usage: echo internoetics_get_plusones("http://www.internoetics.com/2012/05/16/count-shares-to-facebook-twitter-linkedin-googleplus/");

Shortcode Functions

The previous version of this post (we updated it in March, 2015) had a single function that aggregated all platforms – not pretty. Here’s individual shortcode functions.

Copy and paste the WordPress function into your theme's functions.php file or, if you sensibly have one installed, your custom functions plugin.

Twitter

Usage of [internoetics_twitter_shares url="http://www.internoetics.com/2012/05/16/count-shares-to-facebook-twitter-linkedin-googleplus/"] returns 37

LinkedIn

Usage of [internoetics_linkedin_shares url="http://www.internoetics.com/2012/05/16/count-shares-to-facebook-twitter-linkedin-googleplus/"] returns 12

Facebook

Usage of [internoetics_facebook_shares url="http://www.internoetics.com/2012/05/16/count-shares-to-facebook-twitter-linkedin-googleplus/"] returns 49

Google Plus One

Usage of [internoetics_plusone_shares url="http://www.internoetics.com/2012/05/16/count-shares-to-facebook-twitter-linkedin-googleplus/"] returns 15

Considerations

Changes don’t occur that often, and social networks aren’t necessarily quick to aggregate your new data. For that reason, it’s best to cache data for at least an hour. This prevents you making repeated and unnecessary requests to the various APIs.

Displaying Your Shares Like Mashable …

A common question in the comments and emails relates to how total shares can be displayed similar to Mashable. If you’re after an easier half-way WordPress solution, see this post.

Mashable Style Share Count

The Mashable Total Share Count

This is an image of the aggregated share count we played with (before we updated the website).

Total Share Count

Example Solution to a Mashable Style Share Count

CountShares.com

We’ve got our own service in Beta. Like our Facebook page and you’ll be the first to get an invitation.

Download the Code

You can download the code here.

31 comments

  1. Ryan

    This is a great article! thank you! Just wondering how i would go about using the shortcode to pull in the count per post that is shared. I am using the short code like so in the loop for posts:

    but I am not getting anything back.

  2. Jason

    Hi!

    Thanks for the great article! I’m using it on my WP website.

    But by the Google+ counts I’m getting this error:
    Fatal error: Call to undefined function curl_init()

    I’m using WP 3.4.

    Can you please help?

    Thanks

  3. Matt

    Is there a way to add all the shares? So instead of having each separate, it would list the total? Something like how Mashable does it, I think.

    • Marty
      Author

      Sure. What I’ve provided above is grossly inefficient… to the point of negligence. I’ve got a database that stores the results of shares etc every few hours. Using my method it’s simply an issue of retrieving the shares attached to each post ID. I’ll try and find the time to post something soon.

      Using the above dodgy method, you can simply use addition to tally the total results. It’ll work, but it’s not best practice.

      It might be best if I email you an example.

  4. Daniel

    Thanks for your post.
    And now I found that… facebook functions should be changed for proper working like below.

    function get_likes($url) {
    $json_string = file_get_contents(‘http://api.ak.facebook.com/restserver.php?v=1.0&method=links.getStats&format=json&urls=’ . rawurlencode($url));
    $json = json_decode($json_string, true);
    return intval( $json[0][‘like_count’] );
    }

  5. Alex

    I know we can get data about multiple urls with one request via facebook api. Can we do the same with twitter?

    Thank you in advance.

  6. sanjiv

    Hi Marty… the information you have shared is really very helpful.. I am a newbie in programming stuffs and already in some confusion after googling enough.. I have a website which hosts voting topics.. each voting topic has a fb like button.. I would like to get the facebook likes for each and every topic available in my website…. lets say I have urls like http://www.test.com/xyz,http://www.test.com/xyzlkasdf, http://www.test.com/anotherVotingTopic , etc etc.. I now want to get the likes for each of these urls in a single JSON file.. can you help me in this…I thank you in advance

  7. punit

    Is there any idea to count the no of email sent to friends. if you have please help me i am getting problem to count he number of email sent to anyone.

    • Marty
      Author

      I suspect the best option is to link a button through a counter of sorts and then just tally the number of ‘clicks’. There’s really no other way unless you want to use a third-part service that’ll just do as I’ve described. Do you want me to provide a sample?

  8. Pingback: Display a Mashable Style Total Number of Social Shares with PHP and WordPress Shortcode

  9. Rachel

    Hi, can you help me get the shares count only for the last 7 days? What should I add to the code above (the one in the beginning of this article)? Thank you!

  10. Pingback: Create Blockquotes with WordPress Shortcode | Internoetics

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">