The troubles of URL shortening service tr.im continue. First the tr.im service went down, seemingly forever, then it was restored, then just recently it taken offline again due to their network provider thinking they were a spam spreading service. I had been using tr.im up until now to publicise posts from my various blogs on Twitter, but with this latest downtime and the reason for it, I decided it was time I hosted my own URL shortening service.
Update: The plugin has been updated to make the YOURLS API URL configurable from the Wordpress administration interface. Read yourls for twitter tools update 1.1
After some initial research, I decided on the YOURLS open source solution as it was simple to set up, had an easy to use administration interface, provided an API and came with a ready made Wordpress plugin. I purchased a new, short domain to host my links and created an index page to display all my shortened URLs for easy access.
The new domain was girv.in, i.e.: my surname. I’d been thinking about this project for a while and originally had the opportunity to purchase the rather excellent domain hack jo.hn, but unfortunately it was taken by the time I actually went to buy it. A shame, but looking on the bright side, girv.in will be 20% of the annual renewal cost of jo.hn.
Although YOURLS came with its own Wordpress plugin, it lacked the hashtag ability of Alex King’s Twitter Tools plugin that I’d been using up until now. Following my experiences with how easy it was to create tr.im plugins for Twitter Tools, I decided to abandon YOURLS own plugin and create a new plugin for twitter tools 2.0 that used YOURLS at girv.in.
Again using the Twitter Tools bit.ly plugin as a base, I simply replaced “bitly” with “YOURLS” in constants, literals, function names and so on and (obviously) modified the short URL generation function to use the YOURLS API instead. Once more, the whole “project” only took around 30 minutes to complete.
The modified short URL generation function is as follows:
define('AKTT_YOURLS_API_HOST', 'your.domain.here'); define('AKTT_YOURLS_API_URL' , 'http://'.AKTT_YOURLS_API_HOST.'/yourls-api.php'); function aktt_yourls_shorten_url($url) { $parts = parse_url($url); if ($parts['host'] != AKTT_YOURLS_API_HOST) { $snoop = get_snoopy(); $json = new Services_JSON(); $api = AKTT_YOURLS_API_URL.'?action=shorturl'; $api .= '&url='.urlencode($url); $api .= '&format=json'; $user = get_option('aktt_yourls_api_user'); $pass = get_option('aktt_yourls_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->shorturl; } return $url; } add_filter('tweet_blog_post_url', 'aktt_yourls_shorten_url');
YOURLS is generally self-hosted, so you must modify the initial AKTT_YOURLS_API_HOST define to match your own domain.
We use the YOURLS JSON API call via a GET request, supplying the URL to be shortened and the optional login credentials. The result of the request is parsed to extract the shortened URL generated by YOURLS, 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-yourls.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 YOURLS 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.



Thanks for putting this out there. I had a question, is there a reason why you didn’t make the YOURLS API URL a setting along with the username and password? I think it would be a lot handier for people to not have to manually edit the plugin file.