• 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)
  • Using regex for this kind of thing is a pain. I think it’s much easier to work with and understand PHP DOMDocument. Here’s an example of doing what you’re trying to do:

    $doc = new DOMDocument();	// Create new DOMDocument
    $doc->loadHTML( $data, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD );	// Load HTML without body or Doctype
    $links = $doc->getElementsByTagName( 'a' );	// Retrieve an array of anchor tags
    
    // Loop through anchor tags
    foreach( $links as $item ) {
    	
    	// IF anchor tag has a target attribute AND the target attribute is '_blank'
    	if( $item->hasAttribute( 'target' ) && '_blank' ===  $item->getAttribute( 'target' ) ) {
    		
    		// Set a rel attribute
    		$item->setAttribute( 'rel', 'nofollow noopener' );
    	}
    		
    }
    
    // Save HTML
    $data = $doc->saveHTML();

    I believe that DOMDocument is an extension that needs to be enabled on the server but most servers should have it enabled. The above works in my test case.

    Thread Starter triplecomp

    (@triplecomp)

    Would this slow the site down in anyway? I want it so that it is like they were always there to begin with

    I don’t see why it would slow it down by any noticeable amount. You could always implement it and see if it makes a different in speed. If it does, back to the drawing board.

    Thread Starter triplecomp

    (@triplecomp)

    Would DOM act in a way that the links will be nofollow before a page is loaded. So it doesn’t have to rely on something loading correctly to show as nofollow. I want it to work in a way similar to the search and replace plugin where it is permanent and like It was always nofollow from the start.

    Possibly if you run it during a wp_insert_post_data filter hook. Whenever the post is being saved you could run the post content through the above code. Of course you should test this in multiple cases to ensure it works as you expect it to ( and keep multiple backups in case of an unforeseen issue ).

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Adding nofollow noopener target blank to external links’ is closed to new replies.