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

  1. Create a new database on your Web Host
  2. Download Lessn here and unzip the files.
  3. Open config.php and amend to include your database details, a login username and password and save the file.
  4. Upload the full lessn folder via FTP.
  5. 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.

  1. Download the plugin here.
  2. Upload wp-lessn directory to /wp-content/plugins/ directory.
  3. Activate the plugin.
  4. Go to the WP Lessn submenu underneath the Plugins menu.
  5. Enter the location of your Lessn installation (eg. http://domain.com/lessn/-/).
  6. Enter your Lessn API key, which can be found on your Lessn homepage (see image below).
  7. Click Save

API Example

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!

Twitter Example

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.

9 Responses

Dalton says:

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

Stu Greenham says:

Yeah the guy who made the plugin basically told me he wasn’t going to fix this issue, but enabling fopen on my hosting worked fine, out of interest how did you workaround with the curl function?

Trevor Gerzen says:

I seem to be getting an error pertaining to fopen also. @Dalton how did you fix it?

@Stu your tweet link is simple and totally works great. Now I would just love to get this lessn plugin worked out

Stu Greenham says:

I had to trawl through the media temple knowledge base to find a solution to this! Here is the article… http://designwoop.com/s/4p

Be sure to check out the workaround, I am not sure if you are on MT but this might be the equivalent on your hosting company with them disabling the use of ‘fopen’

Dalton says:

Hey Trevor,
I followed up with you via email, but in case anyone else needs the file, here it is: http://daltn.com/x/E

I know the developer is working on this, it might be useful to shoot him an email and see how it’s coming along.

Dalton

Dalton says:

Thanks! This post was incredibly useful. I had some problems with WP Lessn on my server (not compatible with fopen()), but I figured it out eventually and am now the owner of my own custom vanity URL shortening service.

Deluxe Blog Tips says:

The shortlinks look cool. I haven’t tried LessN because I always use free shortlink services like bit.ly or tinyurl. Another good service, which we can use with our domain is google shortlinks. Using some php scripts like this maybe make the server works harder. If, we have a high traffic WP blog, I think we should not use this.

Subscribe to DesignWoop and keep up to date. You can subscribe via RSS or Email.

Close
Close