自动给WordPress文章或评论内容的站外链接添加Nofollow属性
出于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 的话,就注销或删除第一行代码.
查看更多关于自动给WordPress文章或评论内容的站外链接添加N的详细内容...
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://www.haodehen.cn/did8814