PHP snippet · Security
Open external links safely in a new tab
Adds target blank and rel noopener noreferrer to external links in your content, closing the tabnabbing hole. Internal links are untouched.
About this php snippet
A link that opens in a new tab with no relation set hands the destination page a reference back to yours, which lets it quietly navigate your tab somewhere else while the visitor is reading. That is the tabnabbing trick, and the noopener relation is the fix.
This walks your post content, finds the links that point at other sites, and sets both attributes for you, so it stops being something an editor has to remember on every link they paste.
What it does
- Finds links to other hosts in the post content
- Opens them in a new tab
- Adds the noopener and noreferrer relations, so the destination cannot reach back into your tab
Good to know
- Only absolute links to other hosts are touched. On-page anchors, relative paths, mail links, and phone links are all skipped before the host is even checked.
- Target and relation are handled separately, so a link where you set one of them by hand keeps exactly what you wrote and only the missing half is filled in
- Subdomains count as external, because the host has to match exactly. Worth knowing if you run a shop or docs site on its own subdomain.
- The noreferrer relation also stops the referrer being sent, so outbound clicks arrive at the destination as direct traffic. If you rely on referrer-based affiliate attribution, drop noreferrer and keep noopener.
- It runs on the content, so it does not reach into menus or widget areas, and it skips feeds



