Skip to content

Automatically Unshortening Links in Wordpress Posts

David Nunez
David Nunez
2 min read
Automatically Unshortening Links in Wordpress Posts

On this site, I have the Broken Links Checker Plugin chugging away in the background. He tirelessly checks and rechecks every link in every post to find URLs that no longer work; pages sometimes just disappear.

In most cases, I’m able to use the Internet Archive Wayback Machine to find archived snapshots of the long-gone links so that the context of my writing archive remains preserved.

I also recently imported all of my old Twitter posts from the past years into my Microblog. Quite a few of those tweets contain links I shared.

At some point, Twitter started automatically shortening links to go through their service. Link shortening has become somewhat commonplace. Lots of companies exist to provide link shortening services (ex.; one of their value propositions is that they provide interesting analytics about the kinds of sites people visit.

Others have written about the problems with link shorteners.

A primary concern is that link shortening creates a single point of failure on the web; this is the antithesis of the way the Internet is supposed to work. If any one of these shortening services goes down, then suddenly those short links point to nothing, effectively breaking the web. This is a real issue; it actually happens.

Furthermore, if the unshortened link goes away, then the short link obfuscates the original source, making archiving nearly impossible.

Brett Terpstra’s StretchLink is an invaluable tool that watches your clipboard for shortened links to expand in the background. However, manually going through the thousands of back posts on my blog to unshorten links by copying and pasting seems a bit obsessive and not really worth my time. Automatic cross-posting happens using IFTTT, and I don’t want to have to “fix” posts that are inbound from Twitter.

So I quickly hacked some code to automatically unshorten links in my posts. It uses a code snippet I found by Jonathon Hill and Gruber’s URL matching regex.

I noticed that the unshortened links tended to have analytics-enabling “UTM” parameters, so I strip those out as well.

A next step would be to somehow “bake” the older links using the Wayback Machine or via downloading snapshots so that they remain in an unchanged format.

Just add this code to the functions.php of your WordPress theme and you’re on your way to abandoning shortened links whenever you save or update a post.


David Nunez Twitter

Dir of Technology at the MIT Museum • Writing about emerging tech's impact on your life • Speculative insights on the intersection of humanity and technology 🤖


Related Posts

Members Public

FCC's Vote against Net Nuetrality is a disservice to museums

Yesterday, the FCC voted to repeal the 2015 Open Internet Order and dismantle the order’s strong net neutrality rules (New York Times summary of what happened). You have probably read about how this might impact broadband quality for things like streaming television or even basic websites via tiered access

FCC's Vote against Net Nuetrality is a disservice to museums
Members Public

Requiem for Rhinos - behind the scenes video

Members Public

Rebooting my Blog for 2015

I have had moments where I’ve made sweeping, public declarations of reboots and fresh starts. These pronouncements have ranged from triumphant to cringe-inducing. This is one of those posts. Existential crises keep me up at night, and If I’m to be honest, have probably haunted me for at