So you want links that are in replied to open in a _blank window? Just certain ones, or every one automatically? This would make any link posted in a reply open in a new windows (i.e. adds target="blank"
to any link)”
https://bbpress.org/forums/topic/open-links-in-new-window#post-15587
I’ve had a read through some of that (brain’s worn out!) but I don’t want to force all links to open new windows, just some to external sites. Users can go external, have a read, then close that window to return to my forum. So something like;
Click here
As none of the members know bbcode it’ll only be me doing it. Well, if I could get it working. Hmm… Sam’s code on one of those threads does it pre-post, so I guess that would work, unless I linked to another post, which I don’t want in a new window.
Ok, so… I added the following code from another post to my bb-tweaks.php;
// add nofollow to externa links only
function target_nofollow_external_only( $text ) {
$domain="travel-writers-exchange.com"; // domain to exclude from target and nofollow
// Strip ALL nofollow
$text = preg_replace('|<a>|iU','</a><a>', $text);
// Add back in when needed
$text = preg_replace('|</a><a>]+http//))(?!([^>]+'.$domain.'))(.+)>|iU', '</a><a rel="nofollow">', $text);
return $text;
}
add_filter('post_text', 'target_nofollow_external_only',999);
It stops my links from working somehow. They highlight, but don’t do anything when clicked. (Try http://mbforum.letsdoo.org/topic/if-your-web-browser-drives-you-mad).
They don’t do anything because they don’t go anywhere. Take a look at the source (from the page you linked):
Get it at <a >http://www.google.co.uk/chrome.</a>
Looks like your regex is stripping out the link altogether.
If it’s just you doing it and never a member, how about the “admin can post anything plugin”?
https://bbpress.org/plugins/topic/admin-can-post-anything/
Thanks, just installed that plugin, but it’s stripping out the URL. I posted;
Click here to get it.
and the other bb-tweak code I mentioned turned it into;
<ablah rel=”nofollow” target=”_blank”>Click here</ablah> to get it.
How about posting it as HTML not bbCode, if it’s just you?
Yay – got it! I removed the ‘fix nofollow & _blank’ code, and discovered how to add the _blank in by experimenting. bbPress wraps the URL in double quotes, so I used this;
Click here to get it.
Works fine. Just got to add the nofollow bit back in, though that affects forum spidering from what one of those other posts says. Well, at least I can open external links in a new window (or tab in Chrome). Thanks for the help!
I just re-added _ck_’s code to bb-tweaks;
add_filter(‘post_text’, ‘bb_rel_nofollow’);
but it isn’t adding the rel=”nofollow”. No worries though, this is fine;
Click here to get it.