wordpress纯代码无插件实现给链接添加nofollow属性的方法

//给标签云里的链接加上 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;
}

 

http://xzh.i3geek.com
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,请不要用于商业用途及非法用途,否则后果自负!
3. 如果你也有好源码或者教程,可以到审核区发布,分享有金币奖励和额外收入!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 如遇到加密压缩包,默认解压密码为"qq301.com",如遇到无法解压的请联系管理员!
资源客是一个优秀的分享资源站,本站资源均为各位友友分享而来,特殊原创会标明如有侵犯版权等可联系删除

资源客 » wordpress纯代码无插件实现给链接添加nofollow属性的方法