0

By default, I am getting WordPress comment's external URL as rel="nofollow". But, I want to make it rel="nofollow noopener" in my theme without plugins. Please help me.

sao
  • 1,835
  • 6
  • 21
  • 40
Aminul
  • 25
  • 4

2 Answers2

0

Solved: I used below code in function.php

function replace_add_blank_noopener($comments_text){
    return str_replace('rel="nofollow"', 'rel="nofollow noopener" target="_blank"', $comments_text);
}
add_filter('comment_text', 'replace_add_blank_noopener');

And called it in comments.php to print.

replace_add_blank_noopener(comment_text()); 
Aminul
  • 25
  • 4
0

Try this:

function add_noopener($content) {
$content = preg_replace_callback('/]*href=["|\']([^"|\']*)["|\'][^>]*>([^<]*)<\/a>/i', function($m) {
    if (strpos($m[1], "YOUR_DOMAIN_ADDRESS") === false)
        return '<a href="'.$m[1].'" rel="noopener" target="_blank">'.$m[2].'</a>';
    else
        return '<a href="'.$m[1].'" target="_blank">'.$m[2].'</a>';
    }, $content);
return $content;
}
add_filter('the_content', 'add_noopener');
Dharman
  • 30,962
  • 25
  • 85
  • 135