Tweet This WordPress Function

I wanted to provide and easy way for visitors to tweet that they were reading an article on my site. At the same time, I didn’t want to slow down the page with any excess javascript. A plain link did just fine, so I wrote this little function for use in my WordPress templates.

Copy this to your theme’s functions.php and then use <?php tweet_this(); ?> in your templates.


function tweet_this() {
global $post;
$tweet = sprintf( __('Currently reading %1$s %2$s'), $post->post_title, wp_get_shortlink() );
echo '<a class="tweethis" href="http://twitter.com/home?status=' . urlencode( $tweet ) . '">Tweet this</a>';
}

Zé gets credit for the il8n tip and thanks to Westi for the wp_get_shortlink tip.

Update: for those that want a nice and pretty Tweet this button, just use some CSS like mine:

a.tweethis {
background: #73A4FA;
padding: 3px 7px;
color: white;
border-radius: 4px;
}
a.tweethis:hover {
background: #333;
}