首页 网站建设 Wordpress 浏览内容
WordPress给评论去除以及添加nofollow
对于文章或评论内容的站外链接,不少人都希望自动添加nofollow属性
将下面的代码添加到主题的functions.php 文件即可:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | add_filter('the_content', 'auto_nofollow'); //nofollow文章内容的站外链接 add_filter('comment_text', 'auto_nofollow'); //nofollow评论内容的站外链接 function auto_nofollow($content) { //return stripslashes(wp_rel_nofollow($content)); return preg_replace_callback('/<a>]+/', 'auto_nofollow_callback', $content); } function auto_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; } </a> |
代码中的第1、3行分别是针对文章内容、评论内容的,请根据自己的需要选择。比如不需要自动给文章内容的站外链接添加 nofollow 的话,就注销或删除第一行代码
为 WordPress 的 Read more 加上nofollow属性
可以将以下代码加到function.php中实现在read more链接上添加nofollow:
1 | function add_nofollow_to_link($link) { return str_replace('<a', ' |
为 WordPress 的评论链接加上nofollow属性:
可以在function.php中加入以下代码来为WordPress的评论链接加上nofollow属性:
1 2 | function add_nofollow_to_comments_popup_link(){ return ' rel="nofollow" '; } add_filter('comments_popup_link_attributes', 'add_nofollow_to_comments_popup_link'); |