jml.is j: prev post | l: next post {effigy} food hacking ? rss

Writing my own URL shortener in WordPress

With a URL like jml.is it’d be a shame if I didn’t at least try to abuse the five letters as a poor man’s URL shortener for my own posts. There are many reasons to use one, 140 characters being the driving force behind the surge we see today, but chief reason for me wasn’t so much to shorten mine but to make them persist if (Odin forbid) I should ever feel compelled to move to a new domain.

I wanted my own solution, not something dependent on Google or bit.ly. I still use goo.gl for anything off-site, but for stuff on jml.is I wanted to use that domain.

Step 1: Collect Underpants Incoming Clicks

I had to decide on a URL scheme. jml.is/x/<id> sounded usable enough and with no real chance that I’ll ever break the 99,999 post barrier that’s a maximum of 14 characters. Sure, I could have gone all out and created 4-byte hashes but that would have required more database and code work than I wanted to invest at this point. The good news is, if I ever do all it takes is to change the /x/ to something new and be back in business without breaking now and future /x/ URLs.

So now we have an incoming link to, say http://jml.is/x/4703. That’s the last post before this one, one you should check out if you haven’t already. Go ahead, I’ll wait. 4703 is the post_ID in WordPress parlance, an auto-incrementing value.

Step 2: Code

Setting up .htaccess to handle things was easy. The only caveat here is that some WordPress plugins slap code before that one that might break it or – in bad cases – overwrite your .htaccess, thinking it’ll always be “stock”. That’s bad behavior to begin with and I’d kill such plugins with a vengeance.

The marked line (4) is what’s important. It rewrites http://jml.is/x/4703 to http://jml.is/index.php?goto=4703. I am specifying index.php because some installations call other files as their index and we don’t want to have to replicate code around. In index.php:

This piece of code goes right on top. When a ?goto= is appended it pushes a “moved permanently”.

Yes, I am aware that at this point I have two redirects, essentially tripling the amount of initial requests. Given that even a light theme like redwash the theme I currently work on and use on this site has ~20 requests, though, this isn’t as big a deal as it was three or five years ago. I survived Stephen Fry, Scoble, and Reddit on a baby spec server…

We’re done. From now on we’ll serve the right page or post no matter if /x/ is used or not.

But wait, there’s more. WordPress has its own shortlink maker, pointing to wp.me (one less character if you’re OK with off-site management of your shortlinks). Overwriting that with our own should only take a second allow us to take advantage of the built-in “get shortlink” and other functionalities…

That, too is simple:

This piece in your functions.php queries the post’s ID and overwrites wp_get_shortlink with our version. That’s the same thing the wp.me shortlink maker does and others like the bit.ly and goo.gl plugins achieve. Only locally. Locally is good.

Add some HTML and PHP along the lines of <a href="<?php wp_get_shortlink(); ?>" title="The Shortlink">Short Link</a> to your theme where needed or wanted and you’re all set. Plugins like the Social plugin use wp_get_shortlink to create tweets and Facebook posts, so it’ll be used there as well.

Have fun shortening.

VN:F [1.9.22_1171]
If you do nothing else, please take a second to rate this post.
Rating: 0.0/5 (0 votes cast)
Google+ User? We have a G+ comment system in beta below.

Leave a Reply

2 Comments

Jason DeFillippo liked this on Facebook.

cygnoir says:

@legerdemain Is there anything you can’t do? Not a rhetorical question here.

Google+ Comments