Integrate Twitter Buttons in Wordpress (with shortened URL)

tweetthisHow to create a "tweet this" button for your blog post in Wordpress? You have many options.

Twitter has become a social network sharing system and you should always provide an easy call-to-action for your readers to share your posts. Also, don't forget to shorten your URL.

The TinyURL Way

There's a great tutorial on Smashing Magazine. However, the specific code I am providing is modified to work with MediaTemple since MediaTemple doesn't support file_get_contents. If you use the code on smashing magazine you will get a Warning: file_get_contents() ...

Put this code in your function.php file

// Create TinyURL for post to Twitter
function getTinyUrl($url) {
	$ch = curl_init();
	$timeout = 5;
	curl_setopt ($ch, CURLOPT_URL, 'http://tinyurl.com/api-create.php?url='.$url);
	curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
	$tinyurl = curl_exec($ch);
	curl_close($ch);
	return $tinyurl;
	}

and in your single.php or wherever you want your "Tweet this" button to show up, put in this code:

<a href="http://twitter.com/home?status=Reading%20@yourusername%20'<?php _e(urlencode(the_title_attribute('echo=0'))); ?>'%20@%20<?php $turl = getTinyUrl(get_permalink($post->ID)); echo $turl ?>" title="Tweet this">Tweet This!</a>

The text after ?status= is the twitter message that your reader will tweet in twitter. You can change it to whatever you like. I like it with my twitter username in it since TinyURL doesn't provide stats, it's a good way to track to see if any of your readers is tweeting your blog posts.

The Bit.ly way

bitly_logo_top

Bit.ly offers stats for your shortened URL with their API.

*** Note | edit July 26,2009 *** Bit.ly's old javascript method still work but it's much nicer to use the API. I have taken down the code for the javascript method.

There are 2 tutorials that shows how to integrate the Bit.ly API with wordpress: popart.com and David Walsh. The 2 scripts are essentially the same (popart.com's script is derived from the code from David)

However, since MediaTemple doesn't support file_get_contents... Here's the code combined with curl function. Put it in your functions.php

/*
	Bit.ly URL Shortener
	Automatically shorten a URL using the Bit.ly API
	based on code from http://davidwalsh.name/bitly-php and http://blogs.popart.com/2009/07/six-wordpress-tips/
*/
function make_bitly_url($url) {
	$login = 'YOUR BIT.LY LOGIN';
	$appkey = 'YOUR BIT.LY API KEY';
	$format = 'xml';
	$version = '2.0.1';
	$bitly = 'http://api.bit.ly/shorten?version='.$version.'&longUrl='.urlencode($url).'&login='.$login.'&apiKey='.$appkey.'&format='.$format;

//get the url
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $bitly);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);

	//parse depending on desired format
	if(strtolower($format) == 'json') {
		$json = @json_decode($response,true);
		return $json['results'][$url]['shortUrl'];
	} else { //xml
		$xml = simplexml_load_string($response);
		return 'http://bit.ly/'.$xml->results->nodeKeyVal->hash;
	}
}

Remember to change your login and API key, both of them you can find on your account page on Bit.ly.

Next step you have to create the actual link of "Tweet this" function. Put it in wherever you want it show up. Possibly in single.php file. You really need to just call <?php echo make_bitly_url(get_permalink()); ?> wherever you want to use the shortened URL.

This is what I do:

<a href="http://twitter.com/home?status=Reading%20%20'<?php _e(urlencode(the_title_attribute('echo=0'))); ?>'%20@%20<?php echo make_bitly_url(get_permalink()); ?>" title="Tweet this" target="_blank">Tweet This!

This creates a nice little tweet with the shortened url along with the title of the post.

After this, just add some pretty twitter graphic and css and you are set. (tasty twitter graphic resources here and here) ((I am using the Flavours Icon set designed by Oliver Twardowski)) I am currently using the bit.ly way for my blog. Yummy data.

The other options

Help! whatever it is that I am trying to say you just don't get it?
Let's try the low tech ways.

Tweetmeme

Tweetmeme wordpress plugin. It'll allow you to add a Retweet button for your readers to retweet. Also will show how many times it has been retweeted.

Tweet This plugin

Tweet This plugin page. This is a very powerful plugin with added features other than just twitter. (delicious, plurk, ) Comes with its own pretty graphics too. Definitely worth using.

Tweet this with Thickbox

Laurent Kretz developed a twitter script where your readers can login to twitter and post the link to your blog post right on your blog (with the help of Thickbox)

Twit it up

It's much like the Tweet this with Thickbox but in a plugin form. Automatically shortens URL using http://is.gd/.

Twit This

Twit this is a simple plug'n play wordpress plugin that adds a "Twit this" button. It also has its own URL shortener. (sounds like sweetener but much sweeter)

personally I haven't tried out these plugins, simply because I prefer more stats

You might also like ...

Spread the love:
  • Digg
  • del.icio.us
  • Facebook
  • Netvibes
  • NewsVine
  • StumbleUpon
  • Technorati
This entry was posted in Social Media
and tagged , , , , , , , , , , ,

blog comments powered by Disqus