海运的博客

WordPress为外部链接自动添加nofollow标签

发布时间:March 17, 2013 // 分类:WordPress // No Comments

WordPress发布文章添加外部链接时为不传递权重可添加nofollow标签,通过在模板functions.php添加以下可为外部链接自动添加nofollow标签。

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;   
}
分类
最新文章
最近回复
  • jiangker: good, very helpful to me
  • fengfeng: N1 armbian 能有编译下内核吗。。我要开启can 不懂怎么操作
  • 1: 方法一ngtcp2要改下:./configure PKG_CONFIG_PATH=/usr/l...
  • 海运: 关闭服务器
  • 海风: override.battery.charge.low以及override.battery.r...
  • koldjf: 不能过滤
  • 杰迪武士: 此文甚好甚强巨,依照此文在树莓派2 + Rasbian上部署成功 感谢博主美文共赏
  • 海运: ups不知有没选项可设置此参数,不过你可以在另外一台电脑上安装nut客户端自动关机。
  • kgami: 想请教一下,设置了的电脑自动关机之后,几秒后UPS怎么也跟着关机了,导致另外一台电脑没关机就断...
  • 海运: 写的很详细了啊,/etc/nut/hosts.conf用以nut-cgi连接nut服务器参数,...