我想知道如何在 WordPress 中自动将所有链接设置为 nofollow。是否有一个 WP 插件可以使我的所有出站链接nofollow
?
非常感谢您的帮助!
最佳答案
如果您为了 SEO 优化而进行此更改,则不能仅使用 JS 进行更改,因为 google 机器人不会读取 Javascript 生成的内容。
但是,您可以在 function.php 中添加过滤器,如下所示:
function rel_nofollow( $content ) {
return preg_replace_callback( '/<a[^>]+/', 'rel_nofollow_callback', $content );
}
add_filter( 'the_content', 'rel_nofollow', 99999 );
function rel_nofollow_callback( $matches ) {
$link = $matches[0];
$exclude = '('. home_url() .'|http://([^.]+\.)?(wp.org|wp.com))';
if ( preg_match( '#href=\S('. $exclude .')#i', $link ) )
return $link;
if ( strpos( $link, 'rel=' ) === false ) {
$link = preg_replace( '/(?<=<a\s)/', 'rel="nofollow" ', $link );
} elseif ( preg_match( '#rel=\S(?!nofollow)#i', $link ) ) {
$link = preg_replace( '#(?<=rel=.)#', 'nofollow ', $link );
}
return $link;
}
此函数将帖子中的所有链接设置为属性:rel="nofollow",但是,如果您想更改所有站点,也许您必须尝试使用 this plugin
关于wordpress - 如何为 WordPress 中的所有出站链接设置 nofollow rel 属性?有插件吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9571210/