Comment cloud with a pencil over it.When I first noticed that my author comment urls were opening in the same window as my blog, I kind of got annoyed by this. Why? Because I don’t want people leaving my blog completely to visit a comment author’s site. Instead, I don’t mind them visiting a comment author’s blog as long as my blog is still open in one of their browser windows.

This, simply put, is the main reason why you wouldn’t want visitors to leave your site completely. Unfortunately, most WordPress blog theme templates don’t set this up for you automatically. If you’re not sure if yours does, just go to one of your single posts and click on one of your comment author links. If it removes your blog completely from the browser, then you don’t have the target=”_blank” attribute set up properly.

Thankfully, I came across a really cool site that explains exactly how to set up your WordPress theme’s function.php file to open comment author links in a new window.

If you’re not concerned about all the ins and outs of how to accomplish this, just follow these instructions:

Once logged into your WordPress admin area, go to Appearance, then Editor. Once there, find your functions.php file. Click on it, and simply paste the following php code at the bottom of the file before the php closing tag (looks like this “?>”.


// Make comment author link URL open in new window function comment_author_link_window() { global $comment; $url = get_comment_author_url(); $author = get_comment_author();

if ( empty( $url ) || 'http://' == $url ) $return = $author; else $return = "<a href='$url' rel='external nofollow' target='_blank'>$author</a>"; return $return; } add_filter('get_comment_author_link', 'comment_author_link_window');

Hope this code is of use to you.


Tags: , , , , ,


10 Comments to “The importance of making your WordPress author comment urls open in a new window and the code to make it happen”

  1. Filip says:

    geez i spent my “life” lookin` for this ha ha..thanks …this is really usefull…

    [Reply]

  2. jimmyj says:

    this doesn’t work.

    [Reply]

    mywebmastertips Reply:

    Are you sure? It may not work with every WordPress theme, all you have to do is copy and paste it to the bottom of your functions.php file.

    You should check to see if any variables in the code aren’t already being used in your template.

    [Reply]

  3. jimmyj says:

    I believe it’s placed correctly…this is the code in my functions.php

    <?php
    if ( function_exists('register_sidebar') )
    register_sidebars(2);

    // Make comment author link URL open in new window
    function comment_author_link_window() {
    global $comment;
    $url = get_comment_author_url();
    $author = get_comment_author();

    if ( empty( $url ) || 'http://&#039; == $url )
    $return = $author;
    else
    $return = "$author“;
    return $return;
    }
    add_filter(‘get_comment_author_link’, ‘comment_author_link_window’);

    ?>

    [Reply]

    mywebmastertips Reply:

    The last part of it should not be $return = “$author”;

    it should be this:


    else
    $return = " <a href='$url' rel='external nofollow' target='_blank'>$author</a>";
    return $return;
    }
    add_filter('get_comment_author_link', 'comment_author_link_window');

    Hope this helps.

    [Reply]

  4. jimmyj says:

    I copied and pasted it exactly…not sure why it would be any different…but your code in the main body of the post is what is there

    [Reply]

  5. [...] 在虎头鱼那看到了通过修改wordpress文件的方法做到评论者主页链接新窗口打开,我嫌麻烦,于是放狗搜索了下,找到了个方法。 [...]

  6. [...] 最后,Dianso同学提供了一个不错的方法,将相应的代码代码复制到function.php文件内(不过建议有相应基础的同学修改),参考地址:请直接点击 ,下个主题考虑用上^^。 « 上一篇:Wordpress下实现图片自动缩放 [...]

  7. S King says:

    THANK YOU! Been looking for this for a while.

    [Reply]

  8. Tom Elliott says:

    Worked a charm and comment links open in new window thanks!

    [Reply]

Leave a Reply

You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>