Publish: 2013-10-14 | Modify: 2018-08-04
To add the "nofollow" attribute to external links in WordPress blog articles, you can add the following code to the functions.php file of your theme. This code will only add the "nofollow" attribute to external links and will not change the attributes of internal links.
// Automatically add nofollow attribute to external links in the content
add_filter('the_content', 'web589_the_content_nofollow', 999);
function web589_the_content_nofollow($content){
preg_match_all('/href="(.*?)"/', $content, $matches);
if($matches){
foreach($matches[1] as $val){
if( strpos($val, home_url()) === false ) {
$content = str_replace("href=\"$val\"", "href=\"$val\" rel=\"external nofollow\"", $content);
}
}
}
return $content;
}
Please note that the original article can be found here. The final interpretation rights belong to the original author. If there is any infringement, please contact QQ:337003006 for removal.
I come from China and I am a freelancer. I specialize in Linux operations, PHP, Golang, and front-end development. I have developed open-source projects such as Zdir, ImgURL, CCAA, and OneNav.