Skip to main content
how to open link in new tab in wordpress

How to Make All Links Open in a New Tab in WordPress without Plugin

Navigating a WordPress website strategically is a key element in enhancing user experience and SEO. A simple yet effective approach is to make all links open in a new tab. This method can be achieved without using any plugins, by adding a snippet of code to your WordPress theme’s functions.php file. This blog post will guide you through this process and explain the SEO benefits of this approach.

Understanding the SEO Benefits

Before diving into the how-to, it’s important to understand why this is beneficial from an SEO perspective. When links open in a new tab, it helps reduce the bounce rate on your site. A lower bounce rate is often interpreted by search engines as a sign of a user-friendly site, which can positively impact your SEO rankings. Additionally, keeping users on your site longer may increase user engagement and time spent on the page, which are positive signals to search engines.

How to Open Links in a New Tab or Window Using funtion.php file

Step 1: The Importance of Backing Up

Before you begin editing any theme files, always back up your WordPress site. This step is crucial as it allows you to revert to the original state if anything goes wrong during the process.

Step 2: Accessing functions.php

Access the functions.php file of your WordPress theme by logging into your WordPress dashboard. Navigate to Appearance > Theme Editor. Locate functions.php in the list of theme files on the right side and click on it to begin editing.

Step 3: Adding the Code

Now, add the following PHP code to your functions.php file. This code is designed to automatically add the target=”_blank” attribute to all links in your WordPress content, thereby making them open in new tabs:

function open_links_in_new_tab($content) {
    $content = preg_replace_callback('{<a(.*?)href="(http.*?)"(.*?)>}i', function($matches) {
        if (strpos($matches[2], site_url()) !== false && !preg_match('!target=!', $matches[0])) {
            return '<a' . $matches[1] . 'href="' . $matches[2] . '" target="_blank"' . $matches[3] . '>';
        } else if (!preg_match('!target=!', $matches[0])) {
            return '<a' . $matches[1] . 'href="' . $matches[2] . '" target="_blank"' . $matches[3] . '>';
        } else {
            return $matches[0];
        }
    }, $content);
    return $content;
}
add_filter('the_content', 'open_links_in_new_tab');

This script will modify your links to open in new tabs without affecting the existing structure of your posts or pages.

Step 4: Save and Test Your Changes

After inserting the code, save your changes by clicking the “Update File” button. It’s important to test the functionality by checking various links on your site to ensure they open in new tabs as expected.

Conclusion

Implementing this simple change can significantly enhance the user experience and contribute positively to your site’s SEO. Users are more likely to stay engaged with your content, and search engines may view your site as more user-friendly, potentially improving your rankings. Remember, this solution does not require any additional plugins, keeping your site streamlined and efficient.