Display Total Number of Social Shares with PHP and WordPress Shortcode

Some time back I provided some dodgy code that would query various social services and provide a total count of the aggregated shares to various platforms. There were more than a few comments that asked how one could display the total number of shares similar to the way in which it’s used on Mashable.com.

If you’re coming back here because your shortcode is no longer working, it’s because SharedCount now requires registration and an API key. Registration is free, but free access still permits up to 10,000 queries a day.

This post will show you how to query the SharedCount API to provide a total social media post count using PHP. We’ll also provide a shortcode function that can be used from within WordPress.

I’ll build a little site sometime soon that emulates the functionality offered by the SharedCount API, but until then, this function will suffice. My site is on a long list of ‘things to do’. Our own service, CountShares.com, is currently undergoing testing. It should be live soon.

If I wanted to share the aggregated share count (from Twitter, Facebook and Google Plus) for this post, I’d simply use the shortcode of [sharedcount] which would display 20. To share the total count from another URL, say, our previous post on social sharing, we’d use the shortcode of [sharedcount url="http://www.internoetics.com/2012/05/16/count-shares-to-facebook-twitter-linkedin-googleplus/"] with the result of 75.

How Mashable Displays a Share Count

We can instruct our code to apply formatting to the outputted text by using f="1" in our shortcode. The default formatting will generate text that tries to replicate the large green share count as displayed on the Mashable website. You can alter it to reflect whatever default presentation you like.

Mashable Style Share Count

Mashable Style Share Count

The shortcode will look as follows:

[sharedcount url="http://www.internoetics.com/2012/05/16/count-shares-to-facebook-twitter-linkedin-googleplus/" f="1"]

The result is as follows:


There are a number of other attributes in the shortcode you may use to style your output. They include the following:

cache – The time to cache your results. Suggest a minimum of 3600 (1 hour)
url = Full URL if not querying the current WP post/page.
f = Set to 1 if outputting formatted text.
bgcolor = Background color.
bordercolor = Border color.
borderwidth = Border width.
bordertype = Border type.
fontcolor = Font color.
fontsize = Font size.
fontweight = Font weight.
padding = Padding.

An exaggerated example of a formatted share count (using Internoetics themed colours) is as follows:

[sharedcount url="http://www.internoetics.com/2012/05/16/count-shares-to-facebook-twitter-linkedin-googleplus/" f="1" bgcolor="#e56c11" fontcolor="#ffffff" bordercolor="#000000" fontsize="60" fontweight="bold" borderwidth="1"]

The result is as follows:


Include the Total Share Count in a Template

As always, if you wanted to include the shortcode into a template, you should use the following:

Include the Total Share Count in a Post or Page

You’ll notice that I include a Twitter share count in the top right of each post container. In case you’re too lazy to scroll up, I’m talking about this…

Twitter Share Count

Twitter Share Count on Each Page

With a bit of styling you can render the total share count in the same location so it displays as follows:

Formatted Share Count

Formatted 'Mashable Style' Share Count

The WordPress Code

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

If you require shortcode to work in a sidebar widget, you'll have to enable the functionality with a filter. If you're using our custom functions plugin, you'll have that feature enabled by default.

The PHP Code (Used Outside of WordPress)

Used outside of WordPress, the following function can be used. Note that I’ve provided for a basic text file cache to ensure you don’t make repeated requests to the API (you’ll have to make sure you correctly define the path to your cache directory).



I’ll have my own service offering similar functionality sometime soon. I’ll try and emulate the code that SharedCount has provided so transitioning over to my platform and API will be painless process. That said, SharedCount seems like an excellent and reliable service.

Make sure you register for the free service, and hardcopy the API key correctly in your code.

As always, comments and questions are welcome. We’ll try and respond in a timely manner.

Shortt URL for this post:


  1. Pingback: Count the Number of Shares to Facebook, Twitter, LinkedIn & Google Plus in a WordPress post (with shortcode)

  2. Vision

    Hi, thank you! Nice code. I have a question:

    It seems that the code above get the number of shares based on the permalink, which is pulled by get_permalink($post->ID);. My website works as a content aggregator, so I want to pull the number of shares of the original post url, which in my website is filled in the post meta links_link_custom. My question is: Is this possible?

    • Marty

      Sure it is. Change the reference you mentioned – if (!$url) $url = get_permalink($post->ID); – to get the URL from your meta_links_link_custom table (or whatever you’ve called it).

      I’m guessing you would have to replace the above with something like this:

      Is this what you meant?

      • Vision

        Awesome! Exactly what I meant. Thank you very much.

        This peace of code was missing:
        $post_id = get_the_ID();

        Before commenting here for your help I was trying only this:
        $url = get_post_meta(“$post_id”, ‘meta_links_link_custom’, true);

        The total share counter is been displayed perfectly in single.php. Now I am wondering how to put this to work in the index.php. For each block of post, like mashable’s homepage ( mashable.com). Do you have the solution to that too? Thanks again.

  3. frank

    Hi, i have the problem with my theme of wordpress.
    the code theme:
    $likes = sprintf(__(‘%s Likes’, ‘dp’), ‘‘.dp_get_post_likes($pid).’‘);

    How do I enter the code?

    should I replace
    but not work

    I hope you can help me.
    thank you!

    • Marty

      Looks like you need to get the URL from the post ID first.

      So, try:

      I can’t be sure unless I see your code…. otherwise I’m just guessing.

  4. Great script, very useful.

    I’m trying to reuse it on a page listing the 10 most shared posts within the last 7 days. Any idea what my orderby value would be e.g. ‘orderby’ => ‘totalshares’, or if i need use something like meta keys?

    Here’s what I have so far;

    $args = array(

    ‘date_query’ => array(
    //set date ranges
    ‘after’ => ‘1 week ago’,
    ‘before’ => ‘today’,
    //allow exact matches
    ‘inclusive’ => true,

    //’meta_key’ => ‘madeup_share_count’,
    ‘orderby’ => ‘totalshares’,
    ‘order’ => ‘DESC’,
    ‘posts_per_page’ => ’10’

    Thanks for any help.

  5. Hi there. Thanks for this code. Is there a way to let it run several times on one page? I want to display the social share count per post in the index- and Archive-pages of my wordpress theme. When I try that, he does calculate the right result for the first post, but the other ones only get the same number displayed, because the function has already been worked through. Is there a way, he can do it for every post on the homepage?

  6. Steve

    Hello, thanks for this tuto, but i need some help here,

    I set up a loop inside of a post. My intention is to show the total number of shares for each posts inside of the loop, so i used this code:

    <?php echo do_shortcode('[totalshares url="]’); ?>

    But it’s not working ! I’m kind of a newbie on these stuffs so how can i make it work ?

  7. Cim

    Hi, great script! I’ve already implemented it on my site. I have a question, this script counts Facebook, twitter, and Google+ shares only right?

  8. Pingback: Mashable style total share counter for WordPress | WordPress Multisite & BuddyPress Tips

  9. Hi Marty, thanks for the awesome code. I have a problem if you’d be so kind to answer it for me.

    The total shares on my individual post (single.php) here is 513 and it displays correctly.

    But on the homepage (index.php) here, it only says 2 shares for all of my posts.

    What could possibly be the reason?

    • Should be the same bug as in my case. On the Homepage it does take the number of the first post correctly, but gives it out at every other post too. It is not able to detect the number for each post seperately. But Service here seems to be dead, so just delete it, I assume.

  10. Dave

    How would I get the url of the current page?

    They don’t have something like this for Drupal 6.

    I’m trying to hard code it into a template file.

  11. There’s an issue with line 38:

    if (!$url) $url = get_permalink($post->ID);

    With debug on it generates the following:
    Notice: Undefined variable: post in …/shares.php on line 38
    Notice: Trying to get property of non-object in …/shares.php on line 38

    But I’m not sure what $url is originally supposed to be defined as in this case.

  12. Pingback: How to Build a Lightweight Social Sharing Plugin - Part I · Erica Franz & Fat Pony

  13. Phil @ Social Buzz

    Really useful code, we’ve been using this to explain to people how to build share count recorders themselves! Thanks Marty :)

    Social Buzz does this for WP too, by the way – with zero pain!

  14. zoomingjapan

    I wanted to insert this into my single.php, but it only displays the shortcode as is (in words, no numbers). If I put the shortcode into a single post manually it works.

    Is there no way to implement this into the single.php?

    Thank you.

    • Marty

      Yes. I’ll post what you’re looking for when I’m at PC (currently on my phone). Search this site for do_shortcode() and it’ll likely return your solution. Shortcode won’t work in a template file.

  15. Pingback: Get individual and total social share count (Facebook, Twitter, Google+. Pinterest) | DL-UAT

Leave a Reply

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