自动给wordpress的文章添加Nofollow属性
浏览次数:1216 分类:建站教程
出于SEO方面的考虑,对于文章或评论内容的站外链接,不少人都希望自动添加nofollow属性,直接将下面的代码添加到主题的 functions.php 文件即可:
add_filter(‘the_content’, ‘auto_nofollow’); //nofollow文章内容的站外链接
add_filter(‘comment_text’, ‘auto_nofollow’); //nofollow评论内容的站外链接
function auto_nofollow($content) {
//return stripslashes(wp_rel_nofollow($content));
return preg_replace_callback(‘/]+/’, ‘auto_nofollow_callback’, $content);
}
function auto_nofollow_callback($matches) {
$link = $matches[0];
$site_link = get_bloginfo(‘url’);
if (strpos($link, ‘rel’) === false) {
$link = preg_replace(“%(href=S(?!$site_link))%i”, ‘rel=”nofollow” $1’, $link);
} elseif (preg_match(“%href=S(?!$site_link)%i”, $link)) {
$link = preg_replace(‘/rel=S(?!nofollow)S*/i’, ‘rel=”nofollow”‘, $link);
}
return $link;
}
代码中的第1、3行分别是针对文章内容、评论内容的,请根据自己的需要选择。比如不需要自动给文章内容的站外链接添加 nofollow 的话,就注销或删除。
上一篇: 小技巧:添加百度异步统计提升网站速度
您好!请登录