Open External Links in New Tab WordPress

When a reader clicks a link to another website from your WordPress post, you face a small choice that has a real effect on your traffic. If the link opens in the same tab, your reader leaves your site completely. If it opens in a new tab, your page stays open in the background and the reader can return to it with one click. That is why most experienced site owners set external links to open in a new tab, while internal links stay in the same tab.
The good news is that you do not need a plugin for this. WordPress already gives you a simple toggle in the editor, and a few lines of code can handle every link on your site automatically. In this guide you will learn the manual method for single links, a JavaScript snippet that covers your whole site, a PHP filter for content links, the menu setting many people miss, and the Elementor toggle for page builder users. You will also learn why the security attributes matter and when you should avoid the shortcut that opens every link in a new tab.
If you are still setting up the basics of your site, start with our guide to the WordPress dashboard explained for beginners so you know where each setting lives. A fast host also helps readers stay longer, so our web hosting guide is worth a read before you change anything else.
How do I open a single external link in a new tab in Gutenberg?
This is the method to use when you only want one or two links to open in a new tab. It takes about ten seconds and it works in every modern WordPress install. First, select the text you want to turn into a link in the Gutenberg editor. Press Ctrl plus K on Windows or Cmd plus K on Mac to open the link popup, or click the link icon in the toolbar. Paste the full URL of the external site, then press Enter to apply the link.
Now click the linked text again to reopen the link popup. Click the pencil icon to edit the link, then click the three dot menu or the advanced options in that small panel. You will see a toggle labelled Open in new tab. Turn it on and save the link. WordPress will now add target equals blank to that link in the HTML, along with the security attribute rel equals noopener. Your reader clicks the link, a new tab opens, and your page stays exactly where it was.
How can JavaScript open all external links in a new tab automatically?
A small JavaScript snippet can check every link on a page after it loads. If the link points to a different domain than your own site, the script adds target equals blank and rel equals noopener noreferrer to it. Internal links are left alone, so your own navigation keeps working normally. This is the cleanest automatic method because it works on posts, pages, widgets, comments, and footer links without editing any content.
Add the code below to your theme using a code snippets plugin or your child theme JavaScript file. Do not add it to the parent theme directly, because a theme update will delete it. Test it on one page first, then check a few links in your browser by right clicking and choosing Inspect to confirm the attributes appear.
document.addEventListener("DOMContentLoaded", function () {
var links = document.querySelectorAll('a[href^="http"]');
links.forEach(function (link) {
if (link.hostname !== window.location.hostname) {
link.setAttribute("target", "_blank");
link.setAttribute("rel", "noopener noreferrer");
}
});
});
The script works by comparing the hostname of each link with the hostname of your own site. If they differ, the link is external. The rel attribute is important here. Without noopener, the new page can access the original page through a browser feature called window opener, which is a security risk. Noreferrer also stops the external site from seeing your page as the referrer in its analytics. Both values together are the safe standard, and you should use them on every external link that opens in a new tab.
How do I use a PHP filter to add target blank to content links?
The PHP method edits the post content before WordPress sends it to the browser. It finds links in your content, checks whether each one points outside your site, and adds the target and rel attributes to the HTML itself. Because the change happens on the server, the attributes are present in the page source, in the HTML that crawlers read, and in the output of any caching plugin.
Add this code to your child theme functions.php file or to a code snippets plugin set to run on the front end only. Always back up your site before editing functions.php, because a small syntax error in that file can lock you out of the admin area. If that happens, remove the code through your hosting file manager or FTP and the site will return to normal.
<?php
add_filter("the_content", function ($content) {
$site = parse_url(home_url(), PHP_URL_HOST);
return preg_replace_callback(
'/<a\s+([^>]*?)href=["\'](https?:\/\/[^"\']+)["\']([^>]*?)>/i',
function ($m) use ($site) {
$host = parse_url($m[2], PHP_URL_HOST);
if ($host && $host !== $site && strpos($m[0], 'target=') === false) {
return '<a ' . $m[1] . 'href="' . $m[2] . '" target="_blank" rel="noopener noreferrer"' . $m[3] . '>';
}
return $m[0];
},
$content
);
});
This filter only touches links inside the_content, which means post and page content. It does not change links in your menus, widgets, or footer, so you still need to handle those separately. It also skips any link that already has a target attribute, so your manual Gutenberg settings are never overwritten. Keep the JavaScript method as a backup if you want widget and comment links covered too, but do not run both methods on the same links in a way that duplicates attributes.
How do I make menu links open in a new tab?
Menu links are not part of your post content, so neither the Gutenberg toggle nor the content filter will touch them. WordPress has a built in setting for this, but it is hidden by default in the classic menu editor. Go to Appearance, then Menus, in your WordPress admin area. Click Screen Options at the top right of the page and tick the box labelled Link Target. A new checkbox will appear under each menu item.
Open the menu item that points to an external site, tick the box labelled Open link in a new tab, and save the menu. The link will now open in a new tab when a visitor clicks it in your navigation. Repeat the step for any other external menu links, such as a link to your social profile, a partner site, or an external shop. Leave internal menu links unticked so your own pages keep opening in the same tab.
What is the base target shortcut and why should I avoid it?
You may find advice online that tells you to add a single line to the head section of your site: a base tag with target equals blank. That one line makes every link on the page open in a new tab, including internal links, admin links, and links inside forms. It is tempting because it is so short, but it is a bad idea for almost every site, and you should avoid it.
The first problem is user experience. Readers expect internal links to open in the same tab so they can use the back button to return. When every internal link opens a new tab, the browser fills up with tabs, the back button stops working as expected, and readers get frustrated. The second problem is control. The base tag gives you no way to make an exception for one link, so you cannot keep a checkout link, a login link, or a download link in the same tab. The third problem is accessibility. Screen reader users and keyboard users rely on predictable link behaviour, and a site wide new tab rule breaks that predictability.
How do I open links in a new tab in Elementor?
Elementor has its own toggle, and it is easy to miss if you are used to Gutenberg. Select any widget that contains a link, such as a Button, Heading, Icon List, or Text Editor widget. Click the link field, paste the URL, and click the gear icon to open the link options. You will see a toggle labelled Open in new window. Turn it on and update the page. Elementor will add target equals blank and the rel attribute for you.
Check each link type separately, because Elementor stores the setting per link, not per page. A button link and an image link on the same page have separate toggles. If you use Elementor for landing pages, set this toggle on every outbound link, including links to a booking tool, a payment page on another domain, or a social profile. For more Elementor techniques, read our guide on how to speed up an Elementor website and our Elementor containers versus sections comparison.
For the official reference on how WordPress handles links in the editor, see the WordPress developer documentation. It explains the block editor link format and the filters you can use to change link output safely.
Should every external link open in a new tab?
Not always. Opening every external link in a new tab is a good default for blog posts, resource lists, and reference links, because the reader is in the middle of reading and you want them to come back. It is less useful for links the reader expects to leave for, such as a link to a store checkout, a sign up page, or a full external article they chose from a list. In those cases the reader has already decided to leave, so opening in the same tab feels natural.
- Open in a new tab: reference links, source links, affiliate links, and links inside a long article.
- Open in the same tab: checkout links, login links, sign up pages, and links from a directory or link list.
- Always add rel equals noopener noreferrer to any link that uses target equals blank.
- Never use a site wide base target tag, because it removes your control over internal links.
Frequently Asked Questions
Does opening links in a new tab help SEO?
No, the target attribute does not directly help or hurt your rankings. Google follows the link and reads the rel attribute in the same way whether the link opens in a new tab or the same tab. The real benefit is user behaviour: readers stay on your site longer when your page remains open, which can improve time on page and reduce bounce.
Do I need a plugin to open external links in a new tab?
No, you do not need a plugin. The Gutenberg toggle covers single links, the JavaScript snippet covers the whole site, and the PHP filter covers post content. A plugin only makes sense if you want extra features such as an icon next to external links or a settings page to exclude certain domains.
What does rel noopener noreferrer actually do?
Noopener stops the new page from accessing your original page through the window opener object, which closes a known security hole. Noreferrer stops the browser from sending your page address as the referrer to the external site. Use both values together on every link that opens in a new tab.
Why is my new tab setting not working?
The most common cause is caching. Clear your caching plugin cache and your browser cache, then test the link again. Other causes are a theme or plugin that rewrites links, a JavaScript error that stops the automatic snippet, or a menu link that still needs its own Link Target checkbox ticked in Appearance then Menus.
Last updated: November 2026.



