First, the code. Add this as a plugin, an mu-plugin, or a snippet straight into functions.php
I’m really excited by all of the upcoming HTML modification API’s in WordPress. Since 6.2 we’ve had the WordPress HTML Tag Processor API which allows parsing HTML without REGEX, and then allows modifying attributes on elements.
That’s basically what this snippet does, parses post content, searches for links, compares the URL to the site URL, and alters the target
attribute if it’s deemed to be an external link.
In WordPress 6.4 there’s updates coming and a new HTML processor which should further simplify markup modification directly from PHP without using REGEX.
Leave a comment