The popuplinks() WordPress PHP function adds element attributes to open links in new tabs.
Usage
popuplinks( $text );
Example:
Input:
popuplinks( 'Please visit <a href="http://www.wordpress.com">WordPress.com</a>.' );
Output:
Please visit <a href="http://www.wordpress.com" target="_blank" rel="external noopener">WordPress.com</a>
Parameters
$text
(string) – Required. Content to replace links to open in a new tab.
More information
See WordPress Developer Resources: popuplinks
Examples
Basic Usage
Add target="_blank"
and rel="external"
to a link.
$text = 'Visit <a href="https://wordpress.org">WordPress.org</a> for more info.'; echo popuplinks($text);
Multiple Links
Add attributes to multiple links in a string.
$text = 'Check out <a href="https://example.com">Example.com</a> and <a href="https://example.org">Example.org</a>.'; echo popuplinks($text);
Preserving Existing Attributes
Preserve existing link attributes when adding new ones.
$text = '<a href="https://wordpress.com" class="my-link">WordPress.com</a>'; echo popuplinks($text);
Ignoring Non-Link Content
Leave non-link content unchanged.
$text = 'Visit our <strong>WordPress</strong> site: <a href="https://wordpress.com">WordPress.com</a>'; echo popuplinks($text);
Using with Filters
Add popuplinks()
to a filter hook to modify content automatically.
add_filter('the_content', 'apply_popuplinks'); function apply_popuplinks($content) { return popuplinks($content); }