Search

How to Open Links in WordPress Comments in a New Tab

By default, WordPress doesn’t allow us to open the links in WordPress comments on a new tab or window. Links will always open in the same tab/window of your site.

Opening links in a new tab offer several advantages for you as a website owner or for your users, such as below:

  • Improve User Experience; users can easily navigate to the original page without losing their place. It can also anticipate the users to click the back button repeatedly.
  • Enable Users to Multi-tasking; they can continue reading the current page while loading the linked content.
  • Retaining Users; when users open the links on a new tab, they can still explore the linked content while keeping the original page open. It will reduce the possibility of them getting distracted or leaving altogether.

This article will show you how to open links in WordPress comments in a new tab without the help of any plugins.

Steps to Open Links in WordPress Comments in a New Tab

Step 1: Add a Comment

First, you need to add a comment. You can comment in any comment section of your pages/posts with a link.

Once you add a comment with a link, you can try to click on it, and the linked content must be open on the same page. Don’t worry; we will fix it in the next step.

Step 2: Add the Code Snippet to the functions.php File

Now, we are moving on to the next step. We will add the simple PHP snippet to the functions.php file.

On your WordPress dashboard, go to Appearance -> Theme File Editor. You can find Theme File Editor under the Tools menu if you’re using a block theme.

Once enter the Theme File Editor page, select the functions.php file. Afterward, paste the PHP snippet below at the end of the functions file content. Next, update the theme file by clicking the Update File button.

function t4a_comment_links_in_new_window($text)
{
    return str_replace('<a', '<a target="_blank"', $text);
}

add_filter('comment_text', 't4a_comment_links_in_new_window');

Note: If you are afraid of breaking your site after adding the new function to the functions.php file, you can create a child theme or custom plugin. Read here to learn more.

Once you add the PHP snippet to the functions.php file, return to the WordPress comment you’ve added in step 1, reload the page, and click the link.

That’s it. Now your links in WordPress comments are open in a new tab.

The Bottom Line

This post demonstrates how simple it is to enable links in WordPress comments to open in a new tab without the help of any plugins. Finally, when working with numerous sources of information at once, opening links in new tabs can increase productivity and simplify navigation. As a result, it can significantly increase both your and your users’ browser efficiency.

We sincerely hope that you found this post to be helpful. If you have any comments or questions, please let us know.

This page may contain affiliate links, which help support our project. Read our affiliate disclosure.
Akbar Padma

Akbar Padma

Akbar is a WordPress expert and a writer staff at WPPagebuilders. He mainly writes about Gutenberg, Elementor, Divi, and other WordPress page builders.
Want to save yearly expense up to $219? why not?

Leave a Comment