收藏WordPress发布文章和评论自动添加Nofollow的方法

2020-4-28 1183

收藏WordPress发布文章和评论自动添加Nofollow的方法!以下代码亲测有效!添加方法WordPress程序模板下的functions.php文件最后边}代码后边!?>代码之前!
[code]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('/<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;
}[/code]


上一篇:wordPress博客利用代码生成txt手动提交sitemap
下一篇:WordPress博客模板11种错误使用之前要看明白