Url Shortener services have increased their presence a lot. And their importance can never be underestimated. There can be many reasons why you wish to use Url Shortener for your wordpress site.

  1. To really shorten your long URLs so people can easily type it on browser
  2. To hide their bad intentions behind a short URL
  3. To look cool 🙂

In this article, I’ll be showcasing a new plugin that I developed to have my own URL Shortener for my wordpress website.

Before we begin, let me show you how your this shortener is going to work.

Original Url: http://yourdomain.com/2016/01/01/this-is-very-very-long-url-that-contains-many-keywords

gets another url

Shortened Url: http://yourdomain.com/yXppz

Looks good? No third party services like bit.ly, goo.gl, wp.me or anything else. It’s you, your domain, your posts, your articles and your own url shortener.

Advantages

Because you are not depending on any third party URL shortening service, and your own wordpress website is your shortening service, nobody else is tracking your visits or link usage. You’ll also be free from any dead links born out of a dead third party service server.

How it works

All we going to do is intercept every incoming request, extract the short slug form the url, check if that slug exist in our database and if it does, perform a 301 permanent redirection to the actual post.

Database

The database structure required by the plugin is really simple. We don’t really need more fields for our data. This is how it looks.

table structure

We just need a single table with 3 columns. All three explain themselves very well, don’t they?

Umm, wordpress hooks right?

Oh yes, we are using three wordpress hooks, but you may wish to use more depending on the features you wish to implement.

  1. wp_loaded: This is called right after wordpress load is complete. It’s at this point, we can safely intercept the incoming request and perform the required redirection.
  2. save_post: This is called whenever the post is saved. It’s here we check whether a given post has a short url assigned or not. And if it’s not, we assign it a new short slug.
  3. edit_form_before_permalink: I used this hook to display the short url on the post edit screen in wordpress administration. Depending on where you wish to show this url, you may need to look for another hook. (custom boxes may be?)

Code Please?

Sure, The plugin consist of two files only. Here’s the code.

Would you mind explaining the code?

Sure, these are the two files for the plugin.

  1. urlshortener.php: This is the main plugin file. As you can see, inside constructor we register the above mentioned hooks.
  2. urlshortenerdb.php: The file for the database CRUD operations. (Not CRUD in it’s real sense, but you get the point right?). If you have written some wordpress plugins, you’d find it family.

Why have you excluded certain characters while generating slug?

Good question. I’ve actually excluded those characters which may cause ambiguity with shared urls because they look same for some fonts. e.g. [1,l,I] look similar, so do [0,o,O]

So it’s better to exclude those characters from URLs, isn’t it?

Share in comments how you think it is and what would you like to improve. So where does http://wittylog.com/5ZEqW go?