About adding the nofollow tag
-
Hi,
I’m using the below code to add target-blank and noopener to all external links on my website. But when I try to add nofollow to a specific link (using WordPress editor), this doesn’t work as the link gets overridden by the code. Is there a way to modify the code such that it doesn’t interfere with adding the nofollow attribute?
function add_target_blank($content) { $content = preg_replace_callback('/]*href=["|\']([^"|\']*)["|\'][^>]*>([^<]*)<\/a>/i', function($m) { if (strpos($m[1], "https://example.com") === false) return '<a href="'.$m[1].'" target="_blank" rel="noopener">'.$m[2].'</a>'; else return '<a href="'.$m[1].'">'.$m[2].'</a>'; }, $content); return $content; } add_filter('the_content', 'add_target_blank');
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
- The topic ‘About adding the nofollow tag’ is closed to new replies.