Using Your Own URL Shortener with Twitter and WordPress
I had to test this a few times live on Twitter before I got it working. I deleted the incorrect entries right after making them, but they still showed up in feeds – sorry if they seemed like spam.
The short URL link that shows up for this post in Twitter is hosted on toppa.com, not bit.ly. Why give SEO love to bit.ly when you keep it for yourself?
I’ve been using Alex King’s Twitter Tools plugin (version 2.0), which automatically tweets my new posts. The full URLs for my posts always exceed Twitter’s 30 character limit for links, so Twitter automatically shortens them to bit.ly redirect links. And I just installed Husani Oakley’s Link Shortcut plugin (version 1.4), which lets you create short URLs on your own site.
So the trick is getting Twitter Tools to use Link Shortcut to create short URLs for your posts. This takes just a couple of edits to the Link Shortcut plugin.
- On line 63 in the LinkshortcutDataManager.php file (in the addLink method), I changed
return true;
toreturn $ident;
. This makes it return the short identifier it generated for your post (I checked all the calls to this method, and they check for any positive return value, so – as currently written – this change doesn’t harm anything). - In linkshortcut.php, I added the following code starting at line 37:
// MT - shorten urls from Twitter Tools add_filter('tweet_blog_post_url', 'mt_tweet_shortcut'); function mt_tweet_shortcut($url) { $LinkshortcutDataManager = new LinkshortcutDataManager(); return 'http://toppa.com/' . $LinkshortcutDataManager->addLink($url, date("Y-m-d G:i:s"), false); }
This takes advantage of the tweet_blog_post_url filter added by Twitter Tools, which lets you use whatever URL shortening service you like. The addLink() method doesn’t return a complete URL, so you have to add your WordPress base URL. The “correct” way to get it is the
get_bloginfo('wpurl')
function, but in this case I want the URL without the “www.” portion (to save 4 precious characters), so I just hardcoded it.The only hitch is there’s no way to pass a custom name for the link to Link Shortcut (the second argument to addLink), so I’m using the current date and time as a name. The admin page for Link Shortcut shows you the full URL for each redirect link it creates, so the name you assign isn’t really that important.
I’ll get in touch with the Link Shortcut author and see if he’s willing to integrate this functionality into the plugin (in a more graceful way than my hack here).
Great idea! I hope Husani takes up the option. It makes a lot of sense given the popularity of twitter tools.