Add Target Blank to Links from Comments and Author Name in WordPress

Posted on January 22, 2014 by Dan Doicaru in WordPress Category

In this WordPress tutorial you will learn how to add target="_blank" to links from comments message (user comment) and to author name link, so you can open these links in a new window.

As an example, when a user will comment to your WordPress post, he is likely to leave a demo link in his comment, also filling the Website Field too (if available) and is annoying when users are leaving your site. You can watch the video tutorial or read further.

Add Target Blank to Links from Comments

To filter and open all links from user comments, you need to open functions.php which is located in your theme folder /wp-content/themes/your-theme. If you don't have it, just create a new file, put it in your theme folder and write the code below:

add_filter( "comment_text", "filter_comment_content" );

function filter_comment_content( $comment_content ){
    return str_replace( "<a ", "<a target='_blank' ", $comment_content );

Don't forget, if you don't have the code surrounded by PHP Tags <?php ... ?> it will not work obviously. To explain in few words, you created a function that will hook in WordPress's core function comment_text, which will search for links in the user comment message and adding the attribute target="_blank".

Open Author Name in a New Window

In the image above with a comment sample, you will see my name name followed by "says". Comment author is linked to his website if the field was completed. Again, if you want to open author's link in a new window, copy the code below in functions.php to filter the link.

add_filter( "get_comment_author_link", "change_comment_author_link" );

function change_comment_author_link( $author_link ){
    return str_replace( "<a ", "<a target='_blank' ", $author_link );

This function is similar to the first one. The only difference is that we will hook another function named get_comment_author_link.

Rate & Share
1 Star2 Stars3 Stars4 Stars5 Stars (2 votes, average: 4.50 out of 5)
Related Articles
  1. Gravatar Icon Dani:

    Very very good! Thanks!!

  2. Gravatar Icon Pukhto Khan:

    Amazing Tut. Thanks for great sharing with easy tips & tricks.

Hint: Wrap your code syntax (html, css or others) between <pre> and </pre> tags. All comments are moderated.

3 + = 10

Subscribe by E-mail

HTML-TUTS is created and maintained by Dan Doicaru.
This website is a rebrand to Extreme Design Studio (, built under WordPress platform.