将下面的代码复制到functions.php中,保存后上传,就可以了,内容中的所有外链,都会自动的添加一个nofollow标签。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
| add_filter('the_content', '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;
} |
转载于:https://www.cnblogs.com/wpsnippet/archive/2012/12/20/2826696.html