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.
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.



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!