WordPress纯代码实现外链新窗口打开

ė 1,537 6 1

用跳转的方式实现文章内外链真的太麻烦,用WordPress纯代码实现外链新窗口打开的同时还给外链添加了nofollow属性,增加了网站的权重。

代码如下没(放在主题function.php内):

add_filter( 'the_content', 'cn_nf_url_parse');  function cn_nf_url_parse( $content ) {  $regexp = "]*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;  }

演示效果:京东

  • 灵魂发问:谁告诉你我国防空洞位置了吗?怕是99%中国人不知道,我也不知道,为什么,因为没告诉咱们啊!怕是很多当官的都不晓得吧!所以别老想着打这个那个,真打起来,你躲都不知道躲哪?最好的位置是到没有军事区,机场等落后位置,因为人家炸这些位置不仅没有利益还会被国际谴责。 --- 2025/03/08
主机推荐
留言区 (1,537阅已有1条留言,俺要占位
:网友留言区仅供网友表达其个人观点,并不表明栖息邦最终立场。



  1. 1F

    这效果真的很不错~

    2018年4月26日 12:28 回复
管理员登录
注册

提示:点击上下面区域关闭

字号调整

View My Stats
空位 空位
本次加载耗时:0.343s