• 欢迎访问刘浪seo网站,跨境电商俱乐部 QQ群262187934-一个无私分享亚马逊干货的群,一群年轻活力的跨境电商人!

自动添加nofollow属性防止WordPress防止权重流失

SEO算法和思维 Charles.z 8年前 (2016-03-23) 3091次浏览 0个评论

我们写文章的时候经常会引用一些外站链接一般是供给用户做参考的,给这些链接添加 nofollow 属性可以减少自己网站权重的流失。手动添加过于麻烦,这里给大家一个自动添加 nofollow 属性的方法,把一下代码加入 functions.php。

add_filter('the_content', 'auto_nofollow');
function auto_nofollow($content) {
return preg_replace_callback('/<a>]+/', '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;
}
要注意的是模板升级或者更换的时候要把这段代码加入新主题。

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('/<a>]+/', '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;
}

刘浪 BLOG , 版权所有丨如未注明 , 均为原创
喜欢 (0)
[13362309299]
分享 (0)
Charles.z
关于作者:

您必须 登录 才能发表评论!