Adding nofollow noopener target blank to external links
-
Hello,
I want to add it to external links only.
Here is the code that is meant to do the job but when I put it in my functions.php folder the site is non responsive. Whats wrong with this code?<?php function replaceAnchorsWithText($data) { $regex = '/<a .* href=["|\']([^"|\']*)["|\'][^>]*>([^<]*)<\/a>/i';//regex if (is_array($data)) { if (strpos($data[1], "https://example.com/") === false) $data= '<a href="'.$data[1].'" rel="nofollow noopener" target="_blank">'.$data[2].'</a>'; else $data='<a href="'.$data[1].'" target="_blank">'.$data[2].'</a>'; } return preg_replace_callback($regex, 'replaceAnchorsWithText', $data); } $output = replaceAnchorsWithText('<a href="https://example.co">A</a> Link Must Be In New line<a href="https://example.com/">B</a>'); echo $output;
Viewing 5 replies - 1 through 5 (of 5 total)
Viewing 5 replies - 1 through 5 (of 5 total)
- The topic ‘Adding nofollow noopener target blank to external links’ is closed to new replies.