For sure you have seen those crazy new URLs like “bit.ly/zaSnA” – those “Short URLs” became famous with twitter and its 140 Char limit. When clicked, the Short URLs redirect you to the original URL (also called “Permanent Link”). Lately i stumbled upon a nice tutorial on how to ease the process of generating a Short URL for your blog posts. It aims to do this job automatically so the reader does not have to care about it. It’s realised by a “shortcode” – thats a wordpress feature which allows the author to run some automatic tasks by writing something like:
Please note: There is also a german version of this page.
Flaws of the existing shortcode
After trying out this tutorial i realized it has some flaws – especially when it comes to performance. It generates the Short URL everytime the article is read. This means every user has to wait some milliseconds (or even seconds if the Short URL provider is under load) until the whole page is displayed just because this job gets done over and over again. Besides that it does not provide much options.
My shot on this
So i decided to write my own shortcode. Its added by:
This will generate an tiny URL of your favorite URL Shortener (default: bit.ly) and inserts it at the point of your article where you added the shortcode – so you can put it in the first line of your post or in the very last, you decide! You can choose from these URL Shorteners: “tr.im”, “is.gd”, “bit.ly”, “snurl.com”, “tinyurl.com” and “u.nu”.
It also has a couple of options (so called “attributes”) to give your readers even more info and at the same time more comfort to you. Every attribute can have the value 1 (= active) or 0 (= not active). Here is a list of the attributes with a short description and default values:
- full – Also shows the Full URL (= permanent link) for this post [Default: 0]
- link – Displays the URL(s) as HTML link(s) [Default: 0]
- txt – Auto adds label(s) in front of the URL(s) saying “Short URL for this post:” and vice versa for Full URLs . [Default: 0]
- short – Shows the Short URL (= permanent link) for this post [Default: 1]
[shorturl txt=1 link=1 full=1]
Result: Label text on, links activated and also show Full URL:
Short URL for this post: http://tr.im/t2PY
Full URL for this post: http://hjacob.com/blog/2009/06/short_url_shortcode_wordpress/
[shorturl short=0 link=1 full=1]
Result:Does not show Short URL, but Full URL and that one as a link
Please note that the order of the attributes doesn’t play a role – the result of the following shortcodes is the same:
[shorturl link=1 full=1] [shorturl full=1 link=1]
Complete auto-modeWith version 1.0 of this plugin a Settings page was added. This offers a complete automatic generation of the Short URL so you have to do absolutely nothing, not even enter the shortcode. It also allows to customize the appearance of the Short URL with HTML and CSS Tags. Funky!
The shortcode is only tested on Wordpress Version 2.8.
You can download the current version of the Short URL Plugin at the wordpress codex repository.
How to Install
- Download the Plugin.
- Upload the shorturl.php into your “wp-content/plugins” folder of your blog.
- Activate the plugin (“Automatic Short URL”) in your Wordpress-Admin Plugins page.
- Start using the shortcode in your articles where ever you like.
- Use the Settings page in the Wordpress-Admin Interface to further customize the Short URL and workflow.
How to auto add the Short URL at a specific place in the theme
If you’d like to always add the Short URL at the same position and to every post – you can place the following PHP Code into the “wp-content/themes/YOUR_THEME/single.php” at the desired position:
<?php echo(do_shortcode("[shorturl txt=1]")) ?>
But remember: you can activate the complete auto mode, which adds the Short URL add the end of an article – this way there is no need to edit the theme.
1.1.1 // 2009-07-19
- Store Settings Bug “undefined function ’empty2zero'” is solved
- Short URLs are based on the “pretty” permalink
- From now on Short URLs get generated after the post is published, this way the URL Shorteners’ Databases don’t get spammed by Draft URLs.
- Added support for “tr.im” Short URLs
- A Settings Page is added.
- Support for 5 different URL Shortener Services (is.gd, bit.ly, u.nu, snurl.com, tinyurl.com).
- Auto-Add functionality (optional!) – no need to write the shortcode by hand anymore.
- Auto added Short URLs support HTML code, so you can tweak their appearance with CSS classes and more.
- The Labels added by the “txt” parameter are now editable.
- A “Reset Settings” function, which removes Settings and URL Caches from posts.
- Inital release.
Donate if you are happy
If you want to show your appreciation or you would like to motivate me to further improve the plugin, you can send me a donation via paypal.
That’s it for now. Thanks for reading – feel free to ask questions in the comments.