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 WordPress.org 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 (jwz.org/blog) 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/ http://har.ms/?p= ) everything seems groovy.

        Now I've got a shortener that competes pretty favorably with bit.ly. Thanks very much for releasing this. Check it out http://har.ms/b/BDM

        • jwz says:

          Cool!

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

  2. DFB says:

    I want this to feed directly into an archive.org 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. Philip Newton says:

    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().

      • Philip Newton says:

        Ah, right - I had seen the "if(absint($p))" bit but had overlooked the "&& get_post($p)" bit.

  5. Joe Crawford says:

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

    Code:

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

    Thanks!

    • 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 http://www.jwz.org/blog/?p=13240780.

        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- http://jwz.org/b/ygnM | grep "rel='short"
          <link rel='shortlink' href='http://jwz.org/b/ygnM' />

          • 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 WordPress.org 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.