Integrating Lessn with WordPress to Create Future Proof Short URL’s
It seem’s like everyone is using Bit.ly these days, and until about a week ago, I was too! One problem is that all these links were creating are relying on Mr Bit.ly hanging around. Okay okay, its unlikely that the popular shortener will go anywhere but I am asking “What If?”.
I decided to convert to Shaun Inman’s simple URL shortener Lessn, which is a self hosted script available for free! Now I have this handling my links and have managed to not only integrate it with my WordPress install but I can also track my views via Mint, here’s how…
Setting Up Lessn
- Create a new database on your Web Host
- Download Lessn here and unzip the files.
- Open config.php and amend to include your database details, a login username and password and save the file.
- Upload the full lessn folder via FTP.
- Visit http://domain.com/lessn/-/ to start shortening URL’s*
*Please note that the lessn folder can and probably should be renamed to something shorter. For example, I use /s/-, which gains me an extra 4 letters. Make sure you rename the folder before step 5.
Setting Up WordPress To Work with Lessn
Now we have Lessn working and you have a self hosted method for creating short URL’s not relying on anyone else, we can integrate it with WordPress thanks to the WP-Lessn plugin by Jay Robinson. This plugin simply creates a short URL using Lessn when a post is published and saves it to a custom field that can then be displayed on your post page wherever your wish.
- Download the plugin here.
- Upload wp-lessn directory to /wp-content/plugins/ directory.
- Activate the plugin.
- Go to the WP Lessn submenu underneath the Plugins menu.
- Enter the location of your Lessn installation (eg. http://domain.com/lessn/-/).
- Enter your Lessn API key, which can be found on your Lessn homepage (see image below).
- Click Save

Now, fingers crossed if everything has been setup right then when you save your next post you will find a custom field type called wp_lessnd_url just under the trackbacks section on your edit post page.
Integrating Lessn Into Your Post Pages
Next step is to use this within your site to allow users to tweet your post using the shorter URL. To do this we will need to first add a Tweet This button or link to your single page template linking it straight to Twitter like this…
<a href="http://twitter.com/home?status=STATUS TO GO HERE" title="Tweet this post">Tweet This Post</a>
Notice where I have put STATUS TO GO HERE, this is where we will need to add the post title and the short url that users can then Tweet straight to Twitter. To add the title this is pretty straight forward, we just use the_title template tag that comes with WordPress, like so….
<a href="http://twitter.com/home?status=<?php the_title(); ?>" title="Tweet this post">Tweet This Post</a>
Next step is to add the short URL that users will see on Twitter and be able to click to see the post. Now we call the wp_lessnd_url to display this but we also need to add a backup incase there isn’t a short URL setup for this post. For this backup we will use the_permalink template tag. Using a simple if statement, this is how we achieve it…
<a href="http://twitter.com/home?status=<?php the_title(); ?> <?php if(get_post_meta($post->ID, "wp_lessnd_url", true)) { echo get_post_meta($post->ID, "wp_lessnd_url", true); } else { the_permalink(); } ?>" title="Tweet this post">Tweet This Post</a>
Thats all thats to it! I know bit.ly links are nice and compact but for me, been able to shrink from…
http://designwoop.com/2010/04/show-me-whatcha-got-html-5-vs-flash-showcase/
To this…
http://designwoop.com/s/3a
Mean’s I can still promote my brand Design Woop but still allow room for the post title in my Tweets. Now when I click on the Tweet This button that I created previously I get taken to Twitter and the post title and short URL are already there and I can just hit Tweet! Boom!

Have Mint?
If you also have Mint installed on your site then you can install Shaun Inmans Lessn Pepper to allow tracking of each URL.
Let me know if you have any trouble installing Lessn Pepper as I won’t be going over it in this post. Any other questions or problems then leave a comment below.
@Stu: Not MediaTemple—I use A2 Hosting. Apparently blocking fopen is somewhat common with hosts, including Media Temple. I replaced it with a curl function. I got in touch with the developer and he said he’s working on it.