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,271 times, 1 visits today)

12 thoughts on “WordPress Hack: Opening Links in New Windows

  1. I edited the file like you said, and nothing changed. Do I need to change the js_quicktags.js file in the plugins folder as well?

    And thanks…I’ve also been looking for a way to do this forever!

  2. You just need to edit quicktags.js in the admin folder.
    I do not know of any js_quicktags.js file in the plugin folder. It may be with some plugin you have installed.

    Did you save / upload the changed quicktags.js file?

  3. Well, I don’t know how it happened but suddenly it started working today! w00t!!!

    Thank you SOOOO much – this is probably the most useful hack ever! ๐Ÿ˜€

  4. ajay, you know what? i didnt know you have made this hack already. I was thinking to ask you to write one actually. May be you already told me about it #-o dont remember yar.

    i’ll put it on my blogs right away.

Comments are closed.