wordpress教程多少都是通用的,如何为文章外链添加target="_blank"、rel="nofollow"标签呢,那么下面老杨以国外主机测评使用的DUX主题测试;
具体方法:
- 找到主题下的
functions.php; - 将以下代码增加到最后面:
// 自动给页面的站外链接添加nofollow属性和新窗口打开
add_filter( 'the_content', 'cn_nf_url_parse');
function cn_nf_url_parse( $content ) {
$regexp = "<a/s[^>]*href=(/"??)([^/" >]*?)//1[^>]*>";
if(preg_match_all("/$regexp/siU", $content, $matches, PREG_SET_ORDER)) {
if( !empty($matches) ) {
$srcUrl = get_option('siteurl');
for ($i=0; $i < count($matches); $i++)
{
$tag = $matches[$i][0];
$tag2 = $matches[$i][0];
$url = $matches[$i][0];
$noFollow = '';
$pattern = '/target/s*=/s*"/s*_blank/s*"/';
preg_match($pattern, $tag2, $match, PREG_OFFSET_CAPTURE);
if( count($match) < 1 )
$noFollow .= ' target="_blank" ';
$pattern = '/rel/s*=/s*"/s*[n|d]ofollow/s*"/';
preg_match($pattern, $tag2, $match, PREG_OFFSET_CAPTURE);
if( count($match) < 1 ) $noFollow .= ' rel="nofollow" '; $pos = strpos($url,$srcUrl); if ($pos === false) { $tag = rtrim ($tag,'>');
$tag .= $noFollow.'>';
$content = str_replace($tag2,$tag,$content);
}
}
}
}
$content = str_replace(']]>', ']]>', $content);
return $content;
}
添加完即可看到效果。
说明:部分主题functions.php进行了加密,可以以添加到相应的主题中的说明文件下,比如DUX可以添加到functions-theme.php最后即可!

国外主机测评



















![搬瓦工:美国新泽西州[USNY_2]机房怎么样?性能、网络、流媒体及游戏区域限制等数据分享-国外主机测评](https://www.veidc.com/wp-content/uploads/2022/06/20220608142817.png)



