tags:

views:

382

answers:

4

Normally, if I want to force a link to open in a new tab (or window) when posting to my blog, I have to either use the link GUI and select "open in new window", or, since I use the HTML view by default, after inserting a link, manually add the "target=" portion of the tag:

<a href="http://link.to/something.great" target="_blank">link text</a>

Is there a plugin or hook location I can use to automatically insert the target attribute, or am I stuck doing it manually?

EDIT: I am looking specifically for a way to modify the link while I am creating it in the editor. If that isn't possible, then maybe a hack on the save process. But I don't want a "run-time" front-end hack, which isn't necessarily permanent.

+1  A: 

If you use jQuery 1.3+ you can easily do this with the following line of JavaScript:

$("a:not([href^='http://your.website-url.here']").attr('target', '_blank');

Just add this to the load() event of jQuery.

Dan Herbert
That's a good idea, but I want to be able to have the target hard-coded. So this would work, but not for my uses. :)
abrahamvegh
+1  A: 

One approach is to modify your functions.php file. See here for an example hook function. Another is to use jQuery.

jonstjohn
This would also work, but again, it's a run-time hack, not a "compile-time" (for lack of a better expression) hack. (I don't mind a hack, just not this kind. ;)
abrahamvegh
A: 

You could do it using javascript fairly easily. Are you wanting to set target on all external links? Or just the ones within the post body?

Either way, here's the jQuery code to do it:

$(document).ready(function(){
    $("#postBody a").attr('target','_blank');
});

Assuming your post body is inside a div with the ID "postBody".

inkedmn
+1  A: 

Then there is always a plugin

http://wordpress.org/extend/plugins/target-blank-in-posts-and-comments/

Bessi
This would be my first choice if what I'm looking for isn't possible.
abrahamvegh