How to Make WordPress Open Links in a New Window?

How to Make WordPress Open Links in a New Window?

This issue bothered me for quite a while at first. Later, after searching online, I found that there are many ways to do it. Broadly speaking, they fall into the following categories.

One point needs to be made first: some of the methods below modify WordPress core files, such as files under the wp-admin or wp-includes directories. Although this works at the time, those changes can easily be overwritten after a WordPress upgrade. A safer approach is to prioritize using a theme, child theme, plugin, or the WordPress editor's built-in "Open in new tab" option.

Method 1: Use <base> to Make Page Links Open in a New Window by Default

Simply add the following code inside the <head> tag in your theme's header.php file:

<base target="_blank">

This method is simple and effective, and this blog used this approach at the time. However, it affects the default opening behavior of almost all relative links on the page, so if your site has interactions such as login, forms, pagination, or anchor jumps, you should test whether the result matches your expectations.

Method 2: Open the Dashboard "Visit Site" Link in a New Window

Open the wp-admin folder and find the admin-header.php file. Look for code similar to the following:

<a href="<?php echo trailingslashit( get_bloginfo( 'url' ) ); ?>" title="<?php esc_attr_e( 'Visit Site' ); ?>">

Insert target="_blank" before the final angle bracket >, then save the file. After modification, it should look something like this:

<a href="<?php echo trailingslashit( get_bloginfo( 'url' ) ); ?>" title="<?php esc_attr_e( 'Visit Site' ); ?>" target="_blank">

Now, when you click the link in the dashboard to open the site's front page, it will open in a new window.

Method 3: Open Links in Comments in a New Window

Open the wp-includes folder and find the comment-template.php file. Look for code similar to the following:

$return = "<a href='$url' rel='external nofollow' class='url'>$author</a>";

Add target="_blank" to the <a> tag:

$return = "<a href='$url' target='_blank' rel='external nofollow' class='url'>$author</a>";

This makes the link associated with a visitor's nickname open in a new window.

If you do not want to modify core files, you can also process comment author links through a filter in your theme's functions.php. The exact code should be confirmed based on your theme and WordPress version, and it is best to make a backup before modifying anything.

Method 4: Open Blogroll Links in a New Window

Open the wp-admin/includes/meta-boxes.php file and find code similar to the following:

<input id="link_target_blank" type="radio" name="link_target" value="_blank" <?php echo
( isset( $link->link_target ) && ( $link->link_target == '_blank' ) ? 'checked="checked"' : '' ); ?> />

Change the code above to:

<input id="link_target_blank" type="radio" name="link_target" value="_blank" checked="checked" />

This makes blogroll links select the new-window option by default. Alternatively, when adding a blogroll link, you can directly choose "open link in a new window" in the relevant options below.

A More Recommended Approach

If you only want external links in a single post to open in a new window, the simplest method is to select the link in the editor and check "Open in new tab." If you are writing HTML manually, you can write it like this:

<a href="https://example.com" target="_blank" rel="noopener noreferrer">Example link</a>

Here:

  • target="_blank" means opening the link in a new window or new tab.
  • rel="noopener noreferrer" helps reduce the risk of the new window accessing the original page in reverse.

If you need all external links across the entire site to open in a new window, it is recommended to handle this uniformly with a theme or plugin rather than directly modifying WordPress core files. That way, your changes are less likely to be lost when WordPress is upgraded.

Leave a Reply