wordpress无插件实现给链接添加nofollow属性

以前都是用插件实现的,但是用插件太占用服务器资源,google了一下,发现别人都总结好了,直接贴出来

[codesyntax lang="php"]

//给标签云里的链接加上 rel="nofollow"
add_filter('wp_tag_cloud', 'cis_nofollow_tag_cloud');
function cis_nofollow_tag_cloud($text) {
return str_replace('<a href=', '<a rel="nofollow" href=', $text);
}

//给 the_tags() 生成的链接 加上 rel="nofollow"
add_filter('the_tags', 'cis_nofollow_the_tag');
function cis_nofollow_the_tag($text) {
return str_replace('rel="tag"', 'rel="tag nofollow"', $text);
}

//给 wp_list_categories() 生成的链接加上 rel="nofollow"
add_filter( 'wp_list_categories', 'cis_nofollow_wp_list_categories' );
function cis_nofollow_wp_list_categories( $text ) {

$text = stripslashes($text);
$text = preg_replace_callback('|<a (.+?)>|i', 'wp_rel_nofollow_callback', $text);
return $text;
}

//给 the_category() 生成的链接加上 rel="nofollow"
add_filter( 'the_category', 'cis_nofollow_the_category' );
function cis_nofollow_the_category( $text ) {

$text = str_replace('rel="category tag"', "", $text);
$text = cis_nofollow_wp_list_categories($text);
return $text;
}

//给 the_author_post_link 生成的链接加上 rel="nofollow"
add_filter('the_author_posts_link', 'cis_nofollow_the_author_posts_link');
function cis_nofollow_the_author_posts_link ($link) {
return str_replace('</a><a href=', '<a rel="nofollow" href=', $link);
}

//给 comments_popup_link_attributes() 生成的链接加上 rel="nofollow"
add_filter('comments_popup_link_attributes', 'cis_nofollow_comments_popup_link_attributes');
function cis_nofollow_comments_popup_link_attributes () {
echo ' rel="nofollow"';
}
//给外部链接添加nofollow属性
add_filter('the_content','web589_the_content_nofollow',999);
function web589_the_content_nofollow($content){
	preg_match_all('/href="(.*?)" rel="external nofollow" /',$content,$matches);
	if($matches){
		foreach($matches[1] as $val){
			if( strpos($val,home_url())===false ) $content=str_replace("href=\"$val\"", "href=\"$val\" rel=\"external nofollow\" ",$content);
		}
	}
	return $content;
}

[/codesyntax]


如何给wordpress站内文章连接加上nofollow属性

我们可以看到,很多人的博客内文章里的链接都是直接的纯文本,这样可以有效的组织权重的流散,原本是很不在意这个的,关于这个文章,是因为百度不收录新文章的地步了,所以决定将站内的文章里的链接全部加上nofollow的标签来阻止权重的流失。

不过显然每个文章里面都通过手动来加上nofollow标签,是我们这些懒人根本不愿干的事情。WordPress 默认的超链接里是没有nofollow标签的,所以我们需要给他增加一个这个功能。

在网上找了几个方法,发现下面这个方法最快最省事。

首先,将以下代码添加到functions.php 文件中

add_filter('the_content', 'my_nofollow');
function my_nofollow($content) {
return preg_replace_callback('/]+/', 'my_nofollow_callback', $content);
}

function my_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;
}

至于放在哪里,只要不是把原来一段代码切断即可,你可以放在标注的前后。

这个代码可以将所有文章内的站外链接全部加上nofollow属性,而站内链接则不限制,而且已发布的文章页有效。


 

米粒在线
  • 本文由 发表于 2013年1月14日19:31:54
  • 转载请务必保留本文链接:https://www.miliol.org/3155.html
生活道理

熬夜如何改变了我们的身体

熬夜的危害有很多,如:经常感到疲劳,免疫力下降:人经常熬夜造成的后遗症,最严重的就是疲劳、精神不振;人体的免疫力也会跟着下降,感冒、胃肠感染、过敏等等自律神经失调症状都会出现。 头痛:熬夜的隔天,上班...
匿名

发表评论

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

评论:1   其中:访客  1   博主  0
    • 189免费空间域名 189免费空间域名 0

      呵呵!我也是使用只更鸟的主题,