[sc:wordpress-category ]One of the things I really get annoyed at with the WordPress Admin menus, is that going to the toolbar and selecting one of the WordPress sites or even “Visit JumbleCat” will load that page in the current window instead of a new one.
Normally I would expect that kind of content to load up in a new browser window so I don’t leave my current page. There’s no place in WordPress to control this, but with a little work it can be fixed:
- Install the “Add Admin JavaScript” from the plugin directory.
- Activate it.
- Go to “Settings”->”Admin JavaScript”.
- Under “Admin JavaScript (in footer)” add the following JavaScript:
jQuery('#wp-admin-bar-wporg > a.ab-item').attr("target","_blank");
jQuery('#wp-admin-bar-documentation > a.ab-item').attr("target","_blank");
jQuery('#wp-admin-bar-support-forums > a.ab-item').attr("target","_blank");
jQuery('#wp-admin-bar-feedback > a.ab-item').attr("target","_blank");
jQuery('#wp-admin-bar-site-name > a.ab-item').attr("target","_blank");
jQuery('#wp-admin-bar-view-site > a.ab-item').attr("target","_blank");
The jQuery selector breaks down like this:
- “#wp-admin-bar-???” identifies which menu item to change.
- ” > ” tells jQuery to look for a child item (the menu items are html “li”‘s, but the actual link is an “a” tag inside of the “li” so we need to change the child item, not the parent).
- “a.ab-item” looks for any “a” tags with a class of “ab-item”.
Once we’ve selected the right “a” link, we add an attribute to it to for the target to be _blank.
The six items select are the 4 external links to WordPress in the WordPress menu as well as the site menu along with the visit site menu item.
If you’d instead like to make ANY link in the admin screen that are not links to another part of the admin pages to open in a new window, you can use the following code, again added to the “Admin JavaScript (in footer)” section:
var root = (location.protocol + '//' + location.host + location.pathname).replace( /wp-admin.*/, "wp-admin");
jQuery('a').each( function() {
if( this.href.indexOf( root ) == -1)
{
this.target = "_blank";
}
});
In this code:
- The root var is built as “http://yoursite.com/wordpress/wp-admin/somefile.php” and then the “replace” strips off the “/somefile.php” so we’re left with a link to the admin pages only for your site.
- The jQuery then selects every “a” tag on the page and executes the inline function .
- The inline function first looks to see if the root string is in the href of the link, if not it adds the target as _blank.
Theoretically this could fail to catch an external link that is passing the admin page in the URL, but that seems unlikely to happen.