Adding rel tag to all external links
-
I want to add a rel tag for all external links in the content. I have found this script and it is doing exactly what I wanted.
add_filter('the_content', 'rel_function'); function rel_function($content) { return preg_replace_callback('/<a[^>]+/', 'rel_all_external_links', $content); } function rel_all_external_links($Matches) { $externalLink = $Matches[0]; $SiteLink = get_bloginfo('url'); if (strpos($link, 'rel') === false) { $externalLink = preg_replace("%(href=\S(?!$SiteLink))%i", 'rel="noopener noreferrer" target="_blank" $1', $externalLink); } elseif (preg_match("%href=\S(?!$SiteLink)%i", $externalLink)) { $externalLink = preg_replace('/rel=\S(?!nofollow)\S*/i', 'rel="noopener noreferrer" target="_blank"', $externalLink); } return $externalLink; }
But how to edit it, so that I can manually add a no-follow tag. With this function no matter if I add a no-follow tag the link is overwritten with “noopener noreferrer”.
Thank you for any help.
Viewing 4 replies - 1 through 4 (of 4 total)
Viewing 4 replies - 1 through 4 (of 4 total)
- The topic ‘Adding rel tag to all external links’ is closed to new replies.