Base64 Shortlinks

I've published a WordPress plugin that does shortlinks in the sexy jwz way.

You may have noticed that the shortlinks on my posts here (which you see in my twits) are really short. I wrote this code a while ago, but I've finally gotten around to packing it up into a plugin, so now your shortlinks can look like this too.

Let me know how it works... I haven't submitted it to the plugin directory yet. Critique of the code from people who understand WordPress internals would also be appreciated.

Update: It is now available in the Plugin Directory as well.

Tags: , , ,
Current Music: Ruby -- Tiny Meat ♬

19 Responses:

  1. Hrm. I'm playing around with this. So... your blog is in a subdirectory ( and the shortlink has to act one level up, on the main domain. How did you rewrite your .htaccess file to make it work?

    I've got the same setup and the plugin isn't doing the trick right out of the box. I've tried a few passes at editing the .htaccess to redirect, but it's not working quite how I pictured yet.

    • jwz says:

      In that case it can't edit ../.htaccess automatically so you have to do that by hand -- I tried to explain that in the text at the bottom where it tells you what to paste there. Suggestions for how to make that clearer?

      • I think I've sorted everything. The newest version of your zip seems to cure everything. Now with the redirect in place ( Redirect /b/ ) everything seems groovy.

        Now I've got a shortener that competes pretty favorably with Thanks very much for releasing this. Check it out

        • jwz says:


          I guess it's possible to leave off the trailing slash and save one character (yielding "") if you know you have nothing in / that begins with "b"...

  2. DFB says:

    I want this to feed directly into an wayback permalink, but I haven't thought that through yet.

  3. Otto says:

    Looks good at a quick run through to me. There may be some minor areas that could be simplified, but I'd have to analyse it a lot more to find them.

    Very nice though, I wouldn't have thought of using base64. When I needed my own shortlinks, I ended up using a different package altogether, although I did have a different domain for it anyway.

  4. The ?p= detection of shortlinks obviously won't work if the shortlink is all-numeric (e.g. '1337' for ID 14122491).

    • jwz says:

      That will only fail if both of those are valid post IDs on your blog. First it checks decimal, then it checks base64. See b64sl_parse_query().

  5. Joe Crawford says:

    Cool. I'm now using this but wanted to add the [link rel=shortlink] microformat.


    /* Add <link rel=shortlink> because it may be useful to someone, somewhere
    function b64sl_add_head() {
    if (is_single()):
    echo "<link rel="shortlink" type="text/html" href="" . wp_get_shortlink() . "" />";
    add_filter ("get_shortlink", "b64sl_pre_get_shortlink", 10, 4);
    add_action("wp_head", "b64sl_add_head");


    • jwz says:

      But that's already there! View Source on this page. I think it comes from wp_shortlink_wp_head in wp-includes/default-filters.php?

      • Joe Crawford says:

        On your page the shortlink inside the link tag is is

        Maybe different when you're logged in?

        On my own site before I added the code I indicated I didn't see a shortlink. Is there a menu option to turn them on somewhere?

        • jwz says:

          Ah, I see the ?p= problem. I didn't understand what $context meant in pre_get_shortlink.

          I don't understand why you aren't getting rel=shortlink in the head by default, though. WP 3.2.1? It's there for me even when not logged in:

          % wget -qO- | grep "rel='short"
          <link rel='shortlink' href='' />

          • Joe Crawford says:

            Yes, I am 3.2.1. And when I turn off your plugin I do now see the shortlink (right after canonical) link tag. Possibly I missed it the first time, but I did a search of the generated source and did not see it.

  6. jwz says:

    Funny story:

    When I submitted this plugin to the Plugin Directory, they wrote back and said, "We have a cron job that searches for and auto-deletes all uploaded plugins that use base64. Can you rewrite it to not use base64?" My response was, "Um, that would kind of defeat the purpose."

    Fortunately this story has the sane happy ending of, "so they just whitelisted it, and it's fine."

    Funny, though!

    • Billy says:

      When I first read the initial post, my immediate thought was "Surely he realises by now that whenever he submits something to an application repository or store or whatever, it just brings him a world of grief!" so this didn't come as a surprise.

      The happy ending, on the other hand, did.