WordPress.org

Support

Support » Plugins and Hacks » Hacks » [Resolved] Add nofollow to links

[Resolved] Add nofollow to links

  • So i try to write my firs function for wordpress and have a problem.

    function add_nofollow_to_links($link) { 
    
    	$domain = str_replace("www.",'', parse_url(site_url(), PHP_URL_HOST));
    	preg_match_all("/<a\s[^>]*href=(\"??)([^\" >]*?)\\1[^>]*>(.*)<\/a>/siU", $link, $matches);
    
    	for ($i=0; $i < count($matches[2]); $i++) {
    		if(strpos($matches[2][$i], $domain) === false){
    			$matches[0][$i] = str_replace('<a', '<a rel="nofollow"', $matches[0][$i]);
    		}
    		$new_link.= $matches[0][$i]."\n";
    	}
    
    	return $new_link;
    
    }
    
    add_filter('the_content','add_nofollow_to_links', 0);

    The problem is in the end of lines… In the $link (via var_dump) i couldn’t found any transfers to a new line, but it transfers links automatically as they are in post editor, but when i return $new_link string, all goes in one line…

Viewing 4 replies - 1 through 4 (of 4 total)
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘[Resolved] Add nofollow to links’ is closed to new replies.
Skip to toolbar