出于SEO考虑到文章或评论内容的站外链接,许多人希望自动添加nofollow属性,直接将以下代码添加到主题中 functions.php 文件即可:
add_filter( he_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(/] /, auto_nofollow_callback, $content); } function auto_nofollow_callback($matches) {$link = $matches[0];$site_link = get_bloginfo(url);if (strpos($link, el) === false) { $link = preg_replace("%(href=S(?!!$site_link))%i", el="nofollow" $1, $link);} elseif (preg_match("%href=S(?!$site_link)%i", $link)) { $link = preg_replace(/rel=S(?!nofollow)S*/i, el="nofollow", $link);}return $link; }
代码中的第1行和第3行分别针对文章内容和评论内容,请根据自己的需要进行选择。例如,不需要自动添加文章内容的站外链接 nofollow。