WordPress function to show a total share counter (FB, Twitter, G+)

February 25, 2013 at 6:45 pm

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: