wordpress评论中的链接自动加上nofollow
方法一: 该方法在打印 a 标签的 title 属性前有以下语句:
echo apply_filters( 'comments_popup_link_attributes', '' );
什么意思? 说明可以通过 comments_popup_link_attributes 为链接加上其他属性,所以我们可以在 function.php 或者在插件中加入以下代码来为 WordPress 的评论链接加上 nofollow,实例代码如下:
function add_nofollow_to_comments_popup_link(){ return ' rel="nofollow" ' ; } add_filter( 'comments_popup_link_attributes' , 'add_nofollow_to_comments_popup_link' );方法二: 如果觉得上面的办法比较麻烦我们可参照下面办法来解决,将下面的代码放到functions.php中,则会给评论中的链接自动加上nofollow,代码如下:
add_filter( 'comment_text' , 'auto_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 ; }查看更多关于wordpress评论中的链接自动加上nofollow - WordPress的详细内容...
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://haodehen.cn/did8622