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;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;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
Stay Updated

You'll notice this site talks about the Thesis theme for Wordpress. Don't know what the heck that is? Find out.



{ 16 comments… read them below or add one }
Top notch tutorial! I’ll absolutely be using this one.
Thanks! Glad to hear it. (Also, what’s up with all the Matts involved with Thesis? :p)
Rock on man! I can finally ditch Tweetmeme. Is what you’re showing at the top there running this code?
No that is TweetMeme haha. I just figured out how to get this going today and just had to share :p
I’m going to be putting it on my other blog tomorrow though, so I’ll probably drop a link here as a little demo.
Nice research
. I haven’t known that is TweetMeme. Maybe this is faster than TweetMeme javascript solution.
Brilliant little tutorial again
Hmm, doesn’t work here. I get an “unauthenticated tweets” as an error ouput instead.
Why don’t you use this here in this blog?
Double check that your Backtype API key is correct.
I’ve checked that more than twice.
Odd, that’s normally what an error like that would mean. Maybe Backtype changed permissions? I’m not sure. I’ll try to take another look at the code if I get a chance, but I’m insanely busy right now. It was working last time I used it (around the time of the post), but that doesn’t really mean anything if it’s not working for you. =/
Thanks. That would be cool.
Maybe it’s because i’ve registered there a few minutes before i grab the API key? Maybe there must be some time between registering and using?
Any progress on figuring this out? I’m getting the Unautheticated tweets message as well.
No but I was on Backtype’s website the other day and they were down with a message saying that they were changing things. That could very well have something to do with their API.
There’s an error in this code:
“http://api.backtype.com/tweetcount.xml?q=$link&key=$key”;
get rid of the
“amp;amp;”
to make it look like:
“http://api.backtype.com/tweetcount.xml?q=$link&key=$key”;
ding dong done!
Thank you for share this. But can you tell me how I put this code in my download page. Please help me.
there is a problem with call to undefined function getbitlyurl( )
?
{ 1 trackback }