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.

yourls plugin for twitter tools 2.0

August 27th, 2009 by John Girvin

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.

YOURLS plugin for Twitter Tools

YOURLS plugin 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?

3 comments on “yourls plugin for twitter tools 2.0”

August 27 2009, 9:38 pm

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.

August 27 2009, 10:33 pm

No reason other than this was just a quick hack to the bit.ly plugin and that only had username and password as settings. It would of course be straightforward to change the API URL to be a setting too – I’ll look into it.

September 1 2009, 11:03 am

I’ve updated the plugin to make the YOURLS API URL a setting now. Read more »

share your thoughts?

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