How to make a “Retweet this post” link

There’s a simple way and a hard way to do this. The hard way also adds a shortened url, allowing the reader to include more of the headline in the 140 characters twitter allows.

First the easy way.

WordPress…

<a href="http://twitter.com/home?status=<?

the_title('','',FALSE);

?>%20<?

the_permalink();

?>" target="_blank">re-Tweet this</a>

Movable Type…

<a href="http://twitter.com/home?status=

<$MTEntryTitle$>%20<$MTEntryPermalink$>

" target="_blank">re-Tweet this</a>

Now the hard way involves going to http://bit.ly/pages/tools/developer-tools/ and joining. Get your API Key and username. Then create the following PHP file (you can name it whatever you want, but just remember the name, because we’ll be using it). Make sure to replace [bitly username] with your bitly username and [API Key] with your bitly API Key.

<?

$link=urldecode($_GET['l']);

$title=urldecode($_GET['t']);

$rtData=implode('',file("http://api.bit.ly/shorten?

version=2.0.1&longUrl=$link&login=

[bitly username]&apiKey=[API Key]"));

$out=json_decode($rtData, true);

$results=$out['results'];

$articleLink=$artData['article_link'];

$myLink=$results[$articleLink];

$shortUrl=$myLink['shortUrl'];

header("Location: http://twitter.com/home?

status=$title%20$shortUrl");

exit();

?>

Once that’s created with the correct username and API Key, save it to your server in a location reachable from the web. Let’s say you save it at http://myserver.com/retweet.php. We’ll then use the following for out links.

WordPress…

<a href="http://myserver.com/retweet.php?t=<?

the_title('','',FALSE);

?>&l=<? the_permalink();

?>" target="_blank">re-Tweet this</a>

Movable Type…

<a href="http://myserver.com/retweet.php?

t=<$MTEntryTitle$>&l=<$MTEntryPermalink$>

" target="_blank">re-Tweet this</a>

This then will take the title and url, convert the url into a shortened url and make your twitter status look something like this…

How to make a "Retweet this post" link http://bit.ly/ddmFZr