johngirvin.com

John Girvin

This is the blog of John Girvin, a software engineer and web developer based in Belfast, Northern Ireland. He draws on over 14 years of hard won, real world experience of different projects, clients and working environments. Visit the rest of the site to find out more and get in touch.

John is currently working with the Art Technology Group (ATG), developing J2EE based, market leading e-commerce technology.

tr.im plugin for twitter tools 2.0

August 3rd, 2009 by John Girvin

Alex King has updated his very useful Twitter Tools Wordpress plugin to version 2.0. Several exciting new features have been added, not least of which is a separate, supporting plugin to add bit.ly URL shortening to the main plugin out of the box.

I’d written earlier about how to add URL shortening to Twitter Tools, giving example code for tr.im which is my preferred URL shortening service.  Fortunately it is straightforward to add the same support to version 2.0 of Twitter Tools. In the previous example I added the new URL shortening functions to the theme’s functions.php file. This time however, in the style of the new Twitter Tools 2.0 bit.ly plugin, I’ll create a whole new tr.im based URL shortening plugin.

Using the provided bit.ly plugin as a base, I simply replaced “bitly” with “trim” in constants, literals, function names and so on and (obviously) modified the short URL generation function to use the tr.im API instead. The whole procedure, including testing, only took around 30 minutes!

The modified short URL generation function is as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
define('AKTT_TRIM_API_SHORTEN_URL', 'http://api.tr.im/api/trim_url.json');
 
function aktt_trim_shorten_url($url) {
	$parts = parse_url($url);
	if ($parts['host'] != 'tr.im') {
		$snoop = get_snoopy();
		$json = new Services_JSON();
		$api = AKTT_TRIM_API_SHORTEN_URL.'?url='.urlencode($url);
		$user = get_option('aktt_trim_api_user');
		$pass = get_option('aktt_trim_api_pass');
		if (!empty($user) && !empty($pass)) {
			$api .= '&username='.urlencode($user).'&password='.urlencode($pass);
		}
		$snoop->agent = 'Twitter Tools http://alexking.org/projects/wordpress';
		$snoop->fetch($api);
		$result = $json->decode($snoop->results);
		$url = $result->url;
	}
	return $url;
}
 
add_filter('tweet_blog_post_url', 'aktt_trim_shorten_url');

We use the tr.im JSON API call via a GET request, supplying the URL to be shortened and the user’s tr.im login credentials. The result of the request is parsed to extract the shortened URL generated by tr.im, which is then returned to the caller. As before, this function works by hooking into the tweet_blog_post_url filter provided by the main Twitter Tools plugin itself, which is called when generating the automatic Twitter message.

Click to download twitter-tools-trim.zip (3Kb).

Since this code is now a plugin, it must be installed into your Wordpress plugins directory and activated in the plugins administration screen as usual. Once activated, you may configure the tr.im login credentials by selecting the Settings -> Twitter Tools menu option in the Wordpress administration section, then entering and saving the required details.

tr.im For Twitter Tools

tr.im For Twitter Tools

All credit is due to Alex King for the bit.ly plugin supplied with Twitter Tools, without which this hack would have been much more difficult.

If you have a Wordpress blog and a Twitter account, Twitter Tools is a very useful plugin to have. Pop over to Alex’s web site and download your copy.

John Girvin
Bookmark and Share

John Girvin is an experienced a software engineer and web developer based in Belfast, Northern Ireland. He draws on over 14 years of hard won, real world experience of different projects, clients and working environments.

John is currently working with the Art Technology Group (ATG), developing J2EE based, market leading e-commerce technology.

If you liked this article, why not subscribe to the RSS feed for more?

5 comments on “tr.im plugin for twitter tools 2.0”

Erick
August 4 2009, 2:01 am

Hi. Came here from Alex King’s twitter tools page. Could you tell me how to modify this code for the snipurl service? I like their http://sn.im service for reasons that you like trim, I suppose. I have thousands of URLs since about five years so am reluctant to move to bitly etc. Could you please guide me for my own customization? I’m only somewhat versed with php and have been using the snipurl api from http://snipurl.com/site/api according to their own code samples but would like to customize this plugin, that’d be awesome. Thanks!

August 4 2009, 8:24 am

Yes, it should be straightforward. First, rename “bitly” references in the standard plugin to “snipurl”. Then, drop your existing snipurl code into a modified aktt_snipurl_shorten_url function as shown above. It should just work!

If you’re using the snipurl example code you will introduce a dependency on the PHP cURL library into your blog setup. You should be able to rewrite the code to use Snoopy in place of cURL to avoid this though, if you prefer.

Erick
August 6 2009, 9:13 am

Thanks so much John. Will give this a swing, and report back my attempts to botch my site :) The SnipURL crew is quite responsive too so we shall see. I have no problems with Curl as it’s standard on all servers that I know of. Cheers!

February 6 2010, 12:34 pm

[...] is a tr.im “plugin plugin” for Twitter Tools by John Girvin, but this did not work for me, don’t know [...]

share your thoughts?

design & content © 2008-2010 john girvin, all rights reserved.