If you have a regular blog and also use the micro-blogging service Twitter, an effective way of driving extra traffic to your regular blog posts is to post (“tweet”) a link and the title of each new article to your Twitter profile. It’s possible to do this manually of course, but the process has a number of steps and can quickly become tiresome if you blog regularly.
Enter Twitter Tools, a Wordpress plugin by Alex King, which integrates Twitter and your Wordpress powered blog in a number of ways. One of Twitter-Tools’ features is, of course, to automatically post a message to Twitter when you post a new article on your blog. These automatic messages contain a short prefix (typically “New post on my blog”), the blog post title and a link to the article itself.
Twitter imposes a 140 character limit on tweets and quite often it can be a problem to fit the prefix, post title and URL into that amount of space. URL shortening services, a number of which have sprung up recently, offer a solution to this issue by replacing your original (often long) blog post URL with a much shorter one, hosted on their domain and which redirects to the original when visited. Some of these services offer value added features such as tracking statistics around clicks on your shortened URLs.
Twitter Tools has built in support for shortening the post URLs using some custom code, but doesn’t come with any such code out of the box. I’ll now show how to easily add URL shortening to Twitter Tools for two of the most popular services, tinyurl and tr.im, though the code could easily be adapted for others.
In your Wordpress theme directory, find the functions.php file and add one of the following code snippets anywhere, depending on which shortening service you want to use.
tr.im example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | /** * twitter-tools filter to use tr.im url shortening service * returns input url unchanged on error * fopen wrappers must be enabled */ function shorten_url_trim($long_url) { $short_url = file_get_contents("http://api.tr.im/api/trim_simple?url=".$long_url."&username=username&password=password"); if (empty($short_url)) { $short_url = $long_url; } return $short_url; } add_filter('tweet_blog_post_url', 'shorten_url_trim'); |
Note the username=username&password=password query parameters on line 8. tr.im allows you to create an account on their service to keep track of all the short URLs you create and statistics about them. If you have an account, put your actual username and password into the parameter string. If you don’t have a tr.im account or don’t want to use it here, you should leave off the last part of the string.
tinyurl example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | /** * twitter-tools filter to use tinyurl url shortening service * returns input url unchanged on error * fopen wrappers must be enabled */ function shorten_url_tinyurl($long_url) { $short_url = file_get_contents("http://tinyurl.com/api-create.php?url=".$long_url); if (empty($short_url)) { $short_url = $long_url; } return $short_url; } add_filter('tweet_blog_post_url', 'shorten_url_tinyurl'); |
These snippets work by hooking their shorten_url_xxx function into a filter provided by Twitter Tools itself. When generating the automatic Twitter message, Twitter Tools invokes the filter hook to generate the URL that will be inserted into the message. Any manner of processing can be carried out in this function and whatever string is returned will be used as the URL for the message.
The tr.im and tinyurl APIs return a simple string containing the shortened URL. This may not be the case for all services, such at bit.ly for example, which may return a structured response encoded as XML, JSON or some other format. Obviously, such responses would need extra code to parse them into a simple URL string as required by the filter interface.
Note that PHP fopen wrappers must be enabled in your installation for this code to function. If this option is not available to you, a more complex solution is required involving the use of either the cURL library or direct socket connections to execute the requests to the shortening APIs.
Below is a tweet showing the tr.im code above in action. This text was generated by Twitter Tools when I posted an article on my Rain Miles Count Double cycling blog, and then automatically published to my Twitter profile. I’ve changed the default prefix, but note the URL: originally http://www.rainmiles.com/archives/beone-storm-10-review.html, but shortened using tr.im to just http://tr.im/icmk which is much easier to read and fits very easily into 140 characters.
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.



URL shorteners are bad, mmkay?
Single point of failure (if tinyurlFoo goes down, so do all the links) and bad for archiving (if tinyurlFoo ceases trading all historical links are lost).