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.

Ante Medic 2 years 2020-12-17T13:10:45-05:00 0 Answers 9 views 0

Leave an answer