External Links
Auto-adds target=_blank and rel=noopener noreferrer to external links in Contensio
About this plugin
External Links
Automatically adds target="_blank" and rel="noopener noreferrer" to every external link in post content. Internal links (same host or subdomains) are left untouched.
Features:
- Zero configuration - install and forget
- Detects external links by comparing the link hostname against
APP_URL - Handles subdomains correctly -
docs.yoursite.comis treated as internal - Strips any existing
targetorrelattributes before adding the new ones - no duplicates - Applied via a content body filter - runs once on the rendered HTML, not per-link on the client
Requirements
- Contensio 2.0 or later
Installation
Composer
composer require contensio/plugin-external-links
Manual
Copy the plugin directory and register the service provider via the admin plugin manager.
No migrations or configuration required.
How it works
The plugin hooks into contensio/content/body - a filter that receives the fully-rendered post body HTML as a string and must return the (optionally modified) string.
A preg_replace_callback walks every <a href="https?://..."> tag in the HTML. For each match:
- The link's hostname is extracted via
parse_url() - If the hostname matches
APP_URL's host (or is a subdomain), the tag is returned unchanged - Otherwise, any existing
targetandrelattributes are removed, andtarget="_blank" rel="noopener noreferrer"are appended
The rel="noopener noreferrer" attribute prevents the opened page from accessing window.opener (security) and prevents the referrer header from being sent (privacy).
Hook reference
| Hook | Type | Description |
|---|---|---|
contensio/content/body |
Filter | Processes post body HTML, adds attributes to external links |