Display how many times a post has been shared on twitter

Display how many times a post has been shared on twitter

If you want to display how many times a post has been shared on Twitter, simply paste the function below into your functions.php file:

function readTwitterShares($url) {
$s = file_get_contents(“http://urls.api.twitter.com/1/urls/count.json”.
“?callback=?&url=”.urlencode($url));
preg_match(“#(“count”):([0-9]*)#”,$s,$ar);
return isset($ar[2]) ? $ar[2] : 0;
}

Then use the following code within the loop to display how many times the current post has been shared on Twitter:

echo readTwitterShares(get_permalink());