Simple WordPress shortlinks

I recently decided to abandon YOURLS as my shortlink solution, opting instead to handle short URLs entirely within WordPress. This choice is not a reflection on YOURLS–it’s still a great product–but rather was borne from my use case for shortlinks; I concluded that, since I only used them in conjunction with WordPress, an external shortlink service was excessive.

While WordPress has provided a native shortlinks feature since the 3.0 release, it uses query strings rather than pretty permalinks. This means that the shortlink for this post would be https://ethitter.com/?p=6360. It’s not the prettiest URL, nor will many systems cache that request; as a result, each shortlink that’s followed would load all of WordPress just to perform a redirect to the post’s full URL. Unsatisfied with Core’s handling but also unwilling to retain YOURLS, I wrote a small WordPress plugin to address my needs.

ETH Simple Shortlinks converts query-based shortlinks to versions that use WordPress’s rewrites system. When active, https://ethitter.com/?p=6360 becomes https://ethitter.com/p/6360/, though the former URL will still resolve as expected.

The plugin overrides the shortlink output WordPress generates, ensuring that the new format is used wherever a short URL is expected. For convenience, the shortlink is also exposed in content lists in the WordPress admin.

By default, the plugin supports posts and pages that are either published or scheduled. Filters allow other post types and statuses to be included.

The plugin is available from the WordPress.org repository: https://wordpress.org/plugins/eth-simple-shortlinks/. Development is on GitHub: https://github.com/ethitter/eth-simple-shortlinks.

6 thoughts on “Simple WordPress shortlinks”

    1. Thanks!

      Wow, everything old is new again! 😆 I even checked the repo for duplicates before submitting mine, but I only searched, I didn’t check tags.

      1. I guess Konstantin was a couple years ahead of me. I think I have some changes on Github that never made it to the repo.

        I’ve occasionally thought of adding a ‘redirect’ post type that would just redirect to external URLs and maybe support custom slugs. It would probably be kind of hard to do without adding a prefix like you have. I’m sure there are already edge cases in my plugin that will trigger a redirect when you don’t want it to.

        1. Oh fun, I’m clearly in good company with this plugin.

          If I find I need ad-hoc short URLs, I’ll probably add another site to my network, map a short domain to it, and use my External Permalinks Redux plugin along with this plugin. Hopefully I don’t need to, though. 😀

          The prefix is definitely a bit of a cheat, but I’m hoping that borrowing Core’s query var for that will minimize conflicts. That said, I have just realized that the rewrite rule doesn’t consider if the permalink structure has a prefix, which I’ll need to correct.

Leave a Reply

Your email address will not be published. Required fields are marked *