27,621 views 10 comments

How to Combine and Display Your Facebook, Twitter, and Comment Counts

Combining your blog’s comment count with social sharing counts such as facebook and twitter can give a better indication to your readers how people truly react to your posts.

While some posts may get few comments, they may get hundreds or thousands of retweets, facebook shares, or bookmarks. Showing the true spread of your writing can inspire people to share and comment more frequently. You could use the big, bulky buttons to display individual facebook and twitter counts, but we’re going to take a look at combining the total numbers of comment, facebook, and twitter counts into one display.

Lets Begin

Open your functions.php file inside your WordPress theme. All the following code should be copy/pasted inside the functions.php file.

Facebook Count

The following code creates a function called fb_count() that will return the total amount of times a post (URL) has been shared.

<br />
function fb_count(){<br />
	$link = get_permalink($post-&gt;ID);<br />
	$content = file_get_contents(&quot;http://api.facebook.com/restserver.php?method=links.getStats&amp;urls=&quot;.$link);<br />
	$element = new SimpleXmlElement($content);<br />
	$shareCount = $element-&gt;link_stat-&gt;total_count;<br />
	return $shareCount;<br />
}<br />

Tweet Count

The following code creates a function called tweet_count() that will return the total amount of times a post (URL) has been tweeted. There are two methods to choose from: Tweetmeme or Backtype.

Tweetmeme Method

Tweetmeme allows for calling their API 250 times per hour before it’ll refuse to return a count. This means, if you get more than 250 views/requests per hour and don’t use any wordpress caching system, you’ll eventually return an error. Although not ideal, in the code below, we surpress the error and return ‘0’ if a count is not returned.

<br />
function tweet_count(){<br />
	$url = get_permalink($post-&gt;ID);<br />
 	$content = @file_get_contents(&quot;http://api.tweetmeme.com/url_info?url=&quot;.$url);<br />
	if (strpos($http_response_header[0], &quot;200&quot;)) {<br />
		$element = new SimpleXmlElement($content);<br />
 		$tweetCount= $element-&gt;story-&gt;url_count;<br />
 		return $tweetCount;<br />
	}<br />
	else{<br />
		return 0;<br />
	}<br />
}<br />

Backtype Method

Backtype is a newer trend that people are using and is slightly faster since it doesn’t return as much data. However, Backtype allows for calling their API only 1000 times per day before it’ll refuse to return a count. This means, if you get more than 1000 views/requests per days and don’t use any wordpress caching system, you’ll eventually return an error. Although not ideal, in the code below, we surpress the error and return ‘0’ if a count is not returned.

Before you can use the Backtype Method, you need to signup and place your API key in the code below where it says ‘YOUR_API_KEY’.

<br />
function tweet_count(){<br />
	$url = get_permalink($post-&gt;ID);<br />
	$key = 'YOUR_API_KEY';<br />
	$content = @file_get_contents(&quot;http://api.backtype.com/tweetcount.xml?q=&quot;.$url.&quot;&amp;key=&quot;.$key);<br />
	if (strpos($http_response_header[0], &quot;200&quot;)) {<br />
		$element = new SimpleXmlElement($content);<br />
		$tweetCount = $element-&gt;tweetcount;<br />
		return $tweetCount;<br />
	}<br />
	else{<br />
		return 0;<br />
	}<br />
}<br />

Combining and Displaying the Results

To display the comment, facebook, and twitter combined totals, we’re going to create a filter. The filter combines the tweet, facebook and comment counts and replaces the default get_comments_number() wordpress call, with the new total.

<br />
add_filter('get_comments_number', 'comment_count');<br />
function comment_count( $count ) {<br />
 global $id;<br />
 $comments_by_type = &amp;separate_comments(get_comments('post_id=' . $id));<br />
 $count = count($comments_by_type['comment']);<br />
 $total = (tweet_count() + fb_count() + $count);<br />
 return $total;<br />
}<br />

Now, we can show the count by using the code below in our theme files (single.php, etc) or anywhere we’d like to show our new comment totals which is something your theme may already do.

</p>
<p>&lt;?php echo get_comments_number(); ?&gt;<br />

Extending this Tutorial

You can easily add more API calls such as delicious bookmark counts or diggs by using the methods above. Due to API limits and how many times you may request a count, it may be a better idea to create a small plugin to store the counts and only update them every few hours. This way, all the counts will always be accurate and you won’t have to call external websites often.

Questions: Do you think it’s a good idea to show total shares/comments rather than comment count alone? Do you think it gives a false impression or accurate description?  You can leave a comment by clicking here.

Did you enjoy this post?

Comment Below , Follow Me on Twitter , Follow Me on Facebook , or Network with Me on Linkedin

-->