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.
Then there is always a plugin
http://wordpress.org/extend/plugins/target-blank-in-posts-and-comments/
One approach is to modify your functions.php file. See here for an example hook function. Another is to use jQuery.
If you use jQuery 1.3+ you can easily do this with the following line of JavaScript:
Just add this to the
load()
event of jQuery.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:
Assuming your post body is inside a div with the ID “postBody”.