Quantcast
Channel: WordPress.org Forums » All Topics
Viewing all articles
Browse latest Browse all 59525

Adding nofollow noopener target blank to external links

$
0
0

Replies: 0

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 all articles
Browse latest Browse all 59525

Trending Articles