WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Add nofollow to links (5 posts)

  1. nonamez123
    Member
    Posted 2 years ago #

    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...

  2. nonamez123
    Member
    Posted 2 years ago #

    solved

  3. webmediatree
    Member
    Posted 2 years ago #

    really its so easy.

  4. nonamez123
    Member
    Posted 2 years ago #

    Yes, my general problem was that i thought, that $link was some local wordpress variable for links because the post, on which i did test was only from links, i understood my fail too late, but when realized the problem, the solution was in one minute )))

  5. Kimmsmith
    Member
    Posted 2 years ago #

    Thank y so much for this...........

Topic Closed

This topic has been closed to new replies.

About this Topic