Wordpress: Auto-Add Short URLs via Plugin

28. June, 2009 · Comments

Wordpress IconFor 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:

[shortcode_name]

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:

[shorturl]

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]

Some examples

[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
http://hjacob.com/blog/2009/06/short_url_shortcode_wordpress/

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-mode

The Settings Page of the Short URL Plugin for Wordpress (released with version 1.0)

The Settings Page of the Short URL Plugin for Wordpress (released with version 1.0)

With 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!

Compatibility

The shortcode is only tested on Wordpress Version 2.8.

Download

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.

Changelog

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.

1.1

  • Added support for “tr.im” Short URLs

1.0

  • 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.

0.1

  • 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.

Kategorie:English · Plugin · Tipps · Wordpress
Tags: · · · · · · ·
  • Add Short URLs to your #Wordpress blog without the hassle http://tinyurl.com/lw4p4w
  • RT @kyrd: Add Short URLs to your #Wordpress blog without the hassle http://tinyurl.com/lw4p4w
  • I am very interested. I have everything ready, and would very much appreciate it.
  • if only their were more quality websites like yours on the internet, please keep up the good work.
  • Currently trying out the Short URL Plugin for Wordpress http://bit.ly/4a5FMj #wp #plugin
  • Works great, thank you
  • Gut!
  • Autos
    great article.thanks!
  • alex
    hi there... great plugin, thanks!

    I'm trying it now but the button 'store settings' is not working for me, so is not taking in any changes.

    is it a mac thing? tried in safari and FF but no joy
  • i have the same problem alex has,
    clicking on »store settings« gives me this error:
    Fatal error: Call to undefined function empty2zero() in /*/*/*/wp-content/plugins/shorturl.php on line 145
  • it would also be great if the plugin could use the »good looking« url as destination ;)
  • @alexander & alex: strange thing, it works without a flaw on my WP 2.8 and 2.8.1 ... but will investigate and send you an email. thanks for the valuable feedback!
  • alex
    Thanks Hendrik, I don't even get an error just a blank space where the options were.

    If it helps, I'm on macOSX 10.5.7 - Tested on Safari and Firefox with same results.

    WP 2.8.1
  • Version 1.1.1 is out with a bugfix of the latest Store Settings Bug (thanks to Alexander and Alex!). Update also includes support of the "pretty" permalinks, which are now used to generate the shortened url.
  • Forexboiff
    Great information, I will be linking back to you and going to look around at your other posts.
  • alex
    It works a treat! Thank you so much!
    I'm reluctant to upgrade to WordPress 2.8.2 now

    :)
  • 西贝小鱼儿
    Great job.
    Thank you very much~
blog comments powered by Disqus