一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

wordpress评论中的链接自动加上nofollow

时间:2013-07-20 编辑:简简单单 来源:一聚教程网

方法一

该方法在打印 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;
}

热门栏目