Skip to:
Content
Pages
Categories
Search
Top
Bottom

Re: Open Links in new window


citizenkeith
Participant

@citizenkeith

@_ck_: My code is still working perfectly for me. Post what you are actually using.

I’m using this (with my domain name inserted):

<?php
/*
Plugin Name: Target Nofollow External Only
Description: append target="_blank" and rel="nofollow" only on external links
Plugin URI:
Author: _ck_
Version: 0.0.1
*/

add_filter('post_text', 'target_nofollow_external_only',999); // unfortunately we have to do this on every page load and not in pre_post

function target_nofollow_external_only( $text ) {
$domain="citizenkeith.com"; // domain to exclude from target and nofollow
$text = preg_replace('|<a (.*)rel=['"]nofollow['"](.+)?>|iU','<a >', $text); // strip ALL nofollow
$text = preg_replace('|<a (?=([^>]+http://))(?!([^>]+'.$domain.'))(.+)>|iU', '<a rel="nofollow" target="_blank">', $text); // add back in when needed
return $text;
}
?>

External links don’t actually work… all the CSS works (rollover changes, etc) but the browser (Firefox and IE for Windows) doesn’t actually recognize a link. I can’t click on it and open it anywhere (same window, new tab, etc).

Skip to toolbar