Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jrevillini/edf08952b2edb906f38644649099b33e to your computer and use it in GitHub Desktop.
Save jrevillini/edf08952b2edb906f38644649099b33e to your computer and use it in GitHub Desktop.
leverage the Redirection plugin database to dynamically replace links within Wordpress content with target URLs
@jrevillini
Copy link
Author

live-replace URLs in Wordpress content using Redirection database

Requires: Redirection
This is a dirty code snippet to live-replace URLs in Wordpress content before a page is served. Please take shots at it and help this become a snippet that works for all situations. Obviously, the best thing would be to actually find/replace strings in the database, however this opens us up to corruption risk and other time-consuming cleanup if it screws up.

This lets the Redirection plugin continue handling uncaught (i.e. external) redirects while live-fixing internal links at render-time*

*this is a horrible if you’re not caching, but then again, what kind of animal are you if you’re not caching to some degree? LOL)

Known Flaws:

  • Doesn’t work on regex matches yet
  • Needs to work for relative URLs (this should work now but I haven’t tested it yet)
  • May or may not work for links with query parameters in them. I have no idea yet.
  • This is all or nothing; if you wanted to keep some links in your content pointing to URLs that redirect, this is not a solution for you.

Benefits of this add-on/snippet

  1. it can be time-consuming to actually find/replace all links in your database with the new URLs
  2. find/replace searches can be greedy and potentially corrupt or change unintended things ... this makes no database changes so it's safer in my opinion
  3. it's usually good to have proper links on your site (i.e. without needing redirection) for SEO reasons and user experience
  4. great for sites that migrated from a static site, different URL structures, etc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment