WordPress內容外鏈加上nofollow標簽

麥子 優(yōu)化維護241字數 543閱讀1分48秒閱讀模式

考慮到SEO的細節(jié),我們對于WordPress網站中的外部鏈接還是需要加上nofollow標簽的,如果手工加太麻煩,這里可以用代碼批量加上。

//批量給外部鏈接加上nofollow
add_filter('the_content','the_content_nofollow',200);
function the_content_nofollow($content){
preg_match_all('/href="(.*?)" rel="nofollow noopener" /',$content,$matches);
if($matches){
foreach($matches[1] as $val){
if( strpos($val,home_url())===false ) //home_url為程序的url地址
$content=str_replace('href="'.$val.'" rel="nofollow noopener" rel="nofollow noopener" ', 'href="'.$val.'" rel="nofollow noopener" rel="nofollow noopener" rel="nofollow noopener" ', $content);
}
}
return $content;
}
投上你的一票
 
  • 本文由 麥子 發(fā)表于 2024年12月31日 17:18:40
  • 轉載請務必保留本文鏈接:http://bjj.org.cn/wpnofollow.html