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

More php snippets

Your EMCP guide

Answers from our docs

YOUR CHAT, YOUR CHOICE

Before we chat

MSR Builds processes your question and up to four recent messages on our server to answer from the EMCP docs. AI is optional and needs your separate permission.

The chat is kept in this page's memory, not saved as a conversation in our database or browser storage. Our hosting and security services process connection information, such as your IP address, to prevent abuse.

Cloudflare Turnstile checks your browser and connection to prevent automated abuse when you send a question. No login is required.

Please leave out personal, sensitive or confidential information, passwords and API keys.

You can withdraw consent and clear this page's chat at any time. This cannot undo a request already sent or erase provider/security records.

Chat privacy, retention & your rights ↗ · Privacy contact

Starting chat consents to this processing. You can browse our docs without using the chat.