Show Number of Retweets in WordPress Without a Plugin

by admin on June 3, 2010 · 17 comments

Show Retweets Without a Plugin in Wordpress

Social proof is a powerful thing. Showing how many people are talking about your article on Twitter can encourage more people to talk about it, that’s a fact. Humans are silly and trusting this way.

The way most people go about it is by installing some sort of a widget or plugin. Now you say, “But Matt, why wouldn’t I want to use a plugin!? Plugins are awesome!”

Well there are a few reasons for this:

  • To allow greater customization and to separate yourself from other sites.
  • To keep your WordPress install lean. Do we really need 1000 plugins mucking up our WordPress install to do even the simplest tasks?
  • To feel awesome. Haha, this is sort of a joke but I’m a huge nerd and get satisfaction out of doing things myself, and you might too. Plus every learning experience is a good one.

Plus it’s extremely easy. How easy? Here’s the code:

The code

<?php

$link = get_permalink($post->ID);
$key = 'YOURKEY';

$url = "http://api.backtype.com/tweetcount.xml?q=$link&amp;amp;key=$key";
$request = new WP_Http;
$result = $request->request( $url );

echo "<span class=\"result\">".$result['body']."</span> tweets";

?>

You’ll need to sign up withBacktype to use get an API key, but it’s a quick process. Once you have that, put it in next to $key.

Where it goes

If you’re using a regular WordPress theme, place that code wherever you’d like it to appear. Usually somewhere in the single.php file, as that’s your post.

If you’re using Thesis, place it in in custom_functions.php like so (changing the hook to where you’d like it to appear:


function twitterstuff() {
?>

//REPLACE THIS LINE WITH THE CODE

 <?php }
add_action('thesis_hook_after_post', 'twitterstuff');

Now customize it!

You really are done, but all this does is place the retweeted in plain text. In the example I put a span around the number, so you can edit span.result in your css file. You really can change it to look however you’d like though, just make sure to put a \ to break quotes when you’re naming something.

Bonus: Link it to Twitter

The cool thing about the plugins, is they allow you to click on them to Retweet your post. You can do that with this too. Go check out the tutorial on Matt Flies on how to shorten urls and come back here.

Place the code he gave you in your functions file (or the custom function for you Thesis users). Now replace the code I gave you earlier with the following:


$link = get_permalink($post->ID);
$key = 'YOURKEY';

$url = "http://api.backtype.com/tweetcount.xml?q=$link&amp;amp;key=$key";
$request = new WP_Http;
$result = $request->request( $url );
$json = $result['body'];

$burl = getBitlyUrl(get_permalink($post->ID));
$posttitle = get_the_title();
echo "<span class=\"result\"><a href=\"http://twitter.com/home?status=RT @themethesis $posttitle: $burl \">" .$result['body']. "</span></a> tweets";

?>

Now clicking the number of tweets will take you to Twitter with an automatically shortened url and the headline pre-populated. Just make sure to change @themethesis to your Twitter username. ;)

You can of course place the link to retweet underneath the number if you’d like, or anywhere else. That’s what’s so great about not using plugins: what you do with this stuff is limited only by your creativity.

There you have it

Only six lines of code to show your tweets in WordPress without a plugin. Now you can show off how popular you are in style.

References:
API / Tweet Count – Backtype
How to Make HTTP Requests – Planet Ozh
Automatically Shorten URLs for Sharing Using bit.ly – Matt Flies

photo by josef dunne

Previous:

Next: