A-A+
wordpress怎么自动给文章外链添加nofollow
今天在跟一站长交换友链时,蒙站长提醒,我的网站外链没有做nofollow标签,其实这个我是不懂得,毕竟我是新站,很多网站知识都还不到位。然后我百度了下nofollow,才发现添加nofollow的重要性。
nofollow是一个html标签的属性值,这个标签的含义是告诉搜索引擎不要追踪此网页的链接,便于集中网站权重,对于我们新站很有用,本来我的博客新站权重就不高,如果每个页面因为外链被降低了权重,对整个博客造成的损失是不小的!
所以我要想办法,给一部分外链加上nofollow,但如果我手动来添加的话,不知道要添加到什么时候,所以我就到网上搜自动为文章添加nofollow的方法,但尝试了很多代码,感觉效果都不好,当然也有插件,比如Nofollow for external link ;DX-SEO插件,但我并不是很想用插件来实现,最终我选择了一段代码来测试。
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就可以了,这段代码能实现自动添加nofollow属性,还能添加target=”_blank” 属性,所有链接都可以新窗口打开,如果你已设置了的将不会重复添加。添加好代码后就可以去网站查看 是否添加nofollow了。
哈哈,看你还挺喜欢研究技术的嘛 后期多交流
好 多教教我