【wordpress主题】wordpress评论中的链接自动加上nofollow

更新时间:2018-04-15    来源:WordPress    手机版     字体:

【www.bbyears.com--WordPress】

方法一

该方法在打印 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("/]+/", "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;
}

本文来源:http://www.bbyears.com/wangyezhizuo/40687.html