DISCLAIMER: this post is older than one year and may not be up to date with latest WordPress version.

Sharedcount.com is a useful website which allow you to get the total likes, shares, tweets, etc for a specific web page. Here’s a super handy function to display how many times a page has been liked/shared/tweeted on your blog.

Simply paste the following function where you want your counter to appear:

function social_shares() {
    $url = get_permalink( $post_id ); 
    $json = file_get_contents("http://api.sharedcount.com/?url=" .
rawurlencode($url));
    $counts = json_decode($json, true);
    $totalcounts= $counts["Twitter"] + 
$counts["Facebook"]["total_count"] +
$counts["GooglePlusOne"];
    echo "<div>$totalcounts Share</div>";
}

Thanks a lot to Davide for submitting this tip!

Tags

4 Comments

  1. instead of file_get_contents you should use wp_remote_retrieve_body

    $json = wp_remote_retrieve_body( wp_remote_get(‘http://api.sharedcount.com/?url=” . rawurlencode($url)));

  2. How this remote_retrieve_body is better than the listed function to retrieve data. Otherwise it is a great idea for us wordpress theme developers.

  3. This code doesnt show anything up.

    Where exactly to put it in? Which file?

    Thanks and greetz!

    M

  4. Hello,

    I am using “po.st” script for the share buttons on my blog, where should I implement this script?

Leave a Comment

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