出于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('/<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; 
}

代码中的第1、3行分别是针对文章内容、评论内容的,请根据自己的需要选择。比如不需要自动给文章内容的站外链接添加 nofollow。


WordPress

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。