Как добавить nofollow к внешним ссылкам в тексте?

//закрытие в nofollow всех внешних ссылок start
function wph_nofollow_external_links($content) {
    $regexp = "<a\s[^>]*href=(\"??)([^\" >]*?)\\1[^>]*>";
    if (preg_match_all("/$regexp/siU", $content, $matches, PREG_SET_ORDER)) {
        if (!empty($matches)) {
            $srcUrl = get_bloginfo('url');
            for ($i=0; $i < count($matches); $i++)
            {
                $tag = $matches[$i][0];
                $tag2 = $matches[$i][0];
                $url = $matches[$i][0];
                $noFollow = '';
                $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(']]>', ']]&gt;', $content);
    return $content;
}
add_filter('the_content', 'wph_nofollow_external_links');
//закрытие в nofollow всех внешних ссылок end

Сниппет довольно объемный, но работает как часы - закрывает только внешние ссылки, оставляя открытыми внутренние (похожие хаки не делают различий между внутренними и внешними ссылками и закрывают их все). Если вы хотите сделать какую-либо ссылку в тексте открытой, то используйте для нее атрибут rel="dofollow" - в этом случае сниппет ничего не будет делать.