在 WordPress 的文章和页面中为外链自动添加 nofollow 属性是一个常见的 SEO 优化方法,可以防止将链接权重传递给外部网站。我们可以手动在主题的 functions.php 文件中添加相关代码来实现自动添加 nofollow,也可以使用插件。
一、主题文件添加代码
//文章与页面外链自动添加nofollow
function add_nofollow_to_external_links($content) {
$regexp = '/<a\s[^>]*href=(\"??)([^\" >]*?)\\1[^>]*>/i';
preg_match_all($regexp, $content, $matches, PREG_SET_ORDER);
foreach ($matches as $match) {
if (!strstr($match[2], home_url())) {
$content = str_replace($match[0], str_replace('<a ', '
}
}
return $content;
}
add_filter('the_content', 'add_nofollow_to_external_links');
Github代码:https://github.com/lingchenzi/blog/blob/main/wordpress/wordpress-nofollow
二、使用插件
1、Nofollow for External Link是一个更简单、专注于添加 nofollow 的插件;
2、WP External Links 是一个功能强大且广受欢迎的插件,不仅能为外链添加 nofollow,还提供额外的功能,例如让外链在新标签页中打开。
按照谷歌和bing的说法:1、谷歌和必应搜索引擎,相比较更重视来自高权重页面的 nofollow 链接,而忽略低权重页面的 follow 链接。2、非 follow 的链接只是不会被搜索引擎主动编入索引,但如果链接已经被编入索引,其他站点内对其的 nofollow 链接,都会根据算法得到部分 SEO 权重。
不管怎么说,外链还是添加nofollow比较好。