Hi @ninjaboi21:
I see @curtismchale gave you a jQuery solution which should work fine. If you'd like a PHP solution, here's one that should work for you too.
Basically the solution comes in two parts; first is to buffer the output using PHP's ob_start() function, which is easy, and then to parse the output in the callback function, which is more involved. I used a not-so-elegant brute-force method; I'm sure there are better ways but the brute force method is what someone who's not so bright like me uses. :) Maybe someone else will come along with something more elegant.
Here's the code; put this in your theme's functions.php file and you should be good to go:
add_action('init','buffer_output_for_links');
function buffer_output_for_links() {
if (!is_admin()) // No need to do in the admin
ob_start('decorate_links');
}
function decorate_links($content) {
$wpurl = get_bloginfo('wpurl');
$parts = explode('<a',$content);
for($index = 1; $index<count($parts); $index++) {
// Break apart on '>' to isolate anchor attributes
$part = explode('>',$parts[$index]);
// Remove all target="_blank"
$part[0] = preg_replace("#\\s*target\\s*=\\s*[\"'].*?['\"]\\s*#",' ',$part[0]);
// Add target="_blank" to all
$part[0] = preg_replace('#href\s*=\s*#','target="_blank" href=',$part[0]);
// Remove target="_blank" from only this domain
$part[0] = preg_replace("#target=\"_blank\" href=(['\"]){$wpurl}#","href=\\1{$wpurl}",$part[0]);
// End this part by reassembling on the '>'
$parts[$index] = implode('>',$part);
}
// Finally reassembling it all on the '<a' leaving a space for good measure
$content = implode('<a ',$parts);
return $content;
}
-Mike Schinkel