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:
[sourcecode lang=“php“]
[shortcode_name]
[/sourcecode]

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:
[sourcecode lang=“php“]
[shorturl]
[/sourcecode]
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

[sourcecode lang=“php“]
[shorturl txt=1 link=1 full=1]
[/sourcecode]

Result: Label text on, links activated and also show Full URL:
[_shorturl txt=1 link=1 full=1]

[sourcecode lang=“php“]
[shorturl short=0 link=1 full=1]
[/sourcecode]
Result:Does not show Short URL, but Full URL and that one as a link
[_shorturl short=0 link=1 full=1]

Please note that the order of the attributes doesn’t play a role – the result of the following shortcodes is the same:
[sourcecode lang=“php“]
[shorturl link=1 full=1]
[shorturl full=1 link=1]
[/sourcecode]

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:

[sourcecode lang=“php“]
<?php echo(do_shortcode("[shorturl txt=1]")) ?>
[/sourcecode]

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.

  • Manometer

    I am very interested. I have everything ready, and would very much appreciate it.

  • hair extensions london

    if only their were more quality websites like yours on the internet, please keep up the good work.

  • baron

    Works great, thank you

  • flash

    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

  • alexander

    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

  • alexander

    it would also be great if the plugin could use the »good looking« url as destination 😉

  • Hendrik Jacob

    @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

  • Hendrik Jacob

    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~

  • Jennason

    it would also be great if the plugin could use the »good looking« url as destination 😉

  • air max shoes

    Hhe into321 article's content rich variety which make us move for our mood after reading this article. surprise, here you will find what you want! Recently, I found some wedsites which commodity is colorful of fashion. Such as xxxxxxxx that worth you to see. Believe me these air max shoes websites won’t let you down.

  • moncler outlet store

    Hhe article's content rich variety which make us move for our mood after reading this article. surprise, here <a href=“ http://music-ofthesun.com/ “ >wslmart.net you will find what you want! Recently, I found some wedsites which commodity is colorful of fashion. Such as xxxxxxxx that worth you to see. Believe me these websites won’t let you down.

  • mobility scooter

    Great plug in jacob very informative..very nice ideas..

  • Llslll

    I absolutely have enjoyable reading your blog. dont stop posting the excellent quality info!

    MBT SHOES

    MBT Womens Boots

    MBT Womens Shoes

    MBT Mens Sandals

    MBT Mens Shoes

  • london extensions

    really good i love the information there should be more sites like this

  • Real2play
  • José Ferreira Netto

    Very god! I loved!

  • Reno Fizaldy

    This is a great plugins, but i have problem when i use it

    the excerpt function in the archive.php has been error, and can't show excerpt posts in archive but show the full posts in archive, i'm very confused about it

    when i'm deactivated this plugins the excerpt function in archive.php back to normal, i think the code on this plugins crash with the excerpt code on function.php

    as you know, i used wordpress 3.1

    can you help me about this problem, i think i can't use another short url plugins because this plugins is so very good

    Thanks

  • Gui Tavares

    Hi there,

    this is a great and useful plugin you just wrote. But I and other people are getting errors because the „file_get_contents()“ function is not always allowed on the server.
    So you might want to try rewriting the code using the „cURL“ method. You can read about it here: http://www.digimantra.com/technology/php/shorten-stats-url-bitly-api-php

    Also note that bitly api is now operating under a different url than the one you used in your plugin code, and a login+APIKey is also needed. So you might want to update that.

  • Hendrik

    Thanks for your feedback! I will try to build an update as soon as i got some square time.

  • Muskan Jain

    SML.xyz is a quick url shortening website. Here you can shorten your url, which are easy to remember.

    Make short links or url using the form above

  • Muskan Jain

    SML.sx is a free tool to quickly create short links and url of any website. You can alsp earn money with

    all links you create per visit. Different countries have different payout rates. You can also advertize

    your website for very low cost. Start earning online today.

  • Muskan Jain

    Copy and paste your long url into the box on the left or signup for a free account and manage all

    your short urls in one place.

  • Muskan Jain

    oURL.in is a quick url shortening website. Here you can shorten your url, which are easy to remember.

    Make short links or url using the form above.