WordPress Hack: Opening Links in New Windows

Having read the tutorial by 21st Century Paladin on Opening Links in New Windows, I dug up an old post of mine in which I had editted the quicktags.js file so that I can insert links via the Quicktags buttons.
I have just modified the hack a little so as to reduce the amount of code being used.

The hack:

1. Open quicktags.js in the wp-admin folder

2. Find:

if (!edCheckOpenTags(i)) {
var URL = prompt('Enter the URL' ,defaultValue);
if (URL) {
edButtons[i].tagStart = '<a href="' + URL + '">';
edInsertTag(myField, i);

3. Replace it with:

if (!edCheckOpenTags(i)) {
var URL = prompt('Enter the URL' ,defaultValue);
if (URL) {
edButtons[i].tagStart = '<a href="' + URL + '"';
if (URL!='http://')
var defaultTarget = prompt('Enter the Target' ,'_blank');
if (defaultTarget) edButtons[i].tagStart += ' target="' + defaultTarget + '"';
} edButtons[i].tagStart +='>';
edInsertTag(myField, i);


The above code creates a prompt for the target when you click on link button in the Quicktags toolbar.
I have set the default target to be _blank using the variable LinkTarget. You can set it to what you want.

If you click Cancel at the prompt for target then no target is inserted into the link, thus giving you the flexibility of choosing which links you want the attribute to be added.

Update: Now, if you press Cancel at the URL prompt, you will not be prompted to insert a target attribute

1. Requires the editting of only one file viz. quicktags.js.
2. It is integrated into the Quicktags Toolbar, which any author is familiar with.
3. Doesn’t require the editting of your theme files and works across all themes. If you get a new theme, you wouldn’t need to reapply the hack.
4. You have the flexibility of choosing which links you want to apply the target attribute.
5. You can choose the value of the target while inserting the link. So, it need not be just _blank.

Note that the above hack requires the editting of WordPress core files and if you are not confident of doing so, please do not.
I have the hack on my blog and it works perfectly ๐Ÿ™‚

Usability Issues:
I know many may complain about usability issues etc. I believe this is a matter of preference. This hack is for those who want to open links in a new browser window and don’t want to type out target="_blank" every single time.
The target attribute is deprecated in XHTML 1.0 Strict and XHTML 1.1. Keep your DOCTYPE to XHTML 1.0 Transitional and you should have no problems.

You can download the hack here.

(Visited 9,523 times, 1 visits today)


Get more stuff like this
in your inbox

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for subscribing.

Something went wrong.