欢迎光临
我们一直在努力

纯代码WordPress站外链自动添加nofollow和blank属性

纯代码wordpress 站外链自动添加nofollow和blank属性

2020-04-08分类:网站教程评论(0)

seo优化 纯代码wordpress 站外链接自动添加nofollow属性方法,做网站的朋友有些可能不知道在发布站外链接的时候不注重nofollownofollow 其实对于网站seo来说是非常好的 可以防止蜘蛛跟踪抓取外链

蜘蛛抓取站外链的话会分散权重,蜘蛛也就会跑到别人家去了,所以nofollow的建设非常的重要,当然了安装启用 nofollow for external link 插件解决

纯代码解决的方法,将下面的代码添加到当前主题的 functions.php 文件即可

functions.php 文件路径:/wp-content/themes/主题/functions.php

// 站外链接自动添加nofollow属性add_filter(the_content,cn_nf_url_parse);functioncn_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;}

自动给文章/页面的站外链接添加nofollow属性(rel=nofollow),并且在新窗口打开这些链接(即添加 target=_blank属性)。如果已经手动给链接添加了 rel=dofollow,就不会添加 rel=nofollow;如果手动添加了 target=_blank,就不会重复添加

分享到:更多(0)
标签:wordpress
上一篇服务器网站防护必备 openrasp -百度安全推出免费开源自我保护下一篇wp-optimize-wordpress网站数据库清理优化插件

评论抢沙发

有人回复时邮件通知我
评论提交中…
提交评论
昵称昵称 (必填)邮箱邮箱 (必填)网址网址

赞(0)
未经允许不得转载:梦马网络 » 纯代码WordPress站外链自动添加nofollow和blank属性
分享到

评论 抢沙发

登录

找回密码

注册