WordPress.org

Ready to get started?Download WordPress

Forums

make link to comment author open in new window? (14 posts)

  1. syncbox
    Member
    Posted 8 years ago #

    When using <?php comment_author_link() ?> in the loop, how can I set this to link to the author's page in a NEW window rather than in the current one?

  2. vkaryl
    Member
    Posted 8 years ago #

    You should be able to add target="blank" or however that's stated. I don't know if you'd need to add it in a specific manner within that author call, or if there'd be somewhere else to do that though.

  3. maerk
    Member
    Posted 8 years ago #

    The template tag doesn't have any parameters, so you can't go <?php comment_author_link("new") ?> or something like that.

    Use <?php comment_author_url(); ?> instead - that outputs just the URL without making the A tag. So you'll need to do:

    <a href="<?php comment_author_url(); ?>" target="_new">Comment author's site</a>

    Although, from personal experience, opening new links in new windows is generally annoying.

  4. syncbox
    Member
    Posted 8 years ago #

    I don't know if you'd need to add it in a specific manner within that author call

    that's what I am asking... is there some place I can find whatever it is that CREATES comment_author_link() to edit that? adding target="_blank" within it produces an error.

    Surely there's someplace that defines what <?php comment_author_link() ?> actually does to write:

    author's name

    ?? where are those files or are they not visible or editable?

  5. syncbox
    Member
    Posted 8 years ago #

    I don't know if you'd need to add it in a specific manner within that author call

    that's what I am asking... is there some place I can find whatever it is that CREATES comment_author_link() to edit that? adding target="_blank" within it produces an error.

    Surely there's someplace that defines what <?php comment_author_link() ?> actually does to write:

    href="http://www.example.com" rel="external nofollow"

    ?? where are those files or are they not visible or editable?

  6. maerk posted the answer for you...

  7. syncbox
    Member
    Posted 8 years ago #

    OK, now I understand. So, if I needed to only show the link IF the author has a url, then what? This is working (thanks!)

    " target="_new"><?php comment_author(); ?>

    but if the author hasn't supplied a url, you get url is not valid and cannot be loaded alert message...

  8. Nieceye
    Member
    Posted 8 years ago #

    I tried all of this and nothing works...any suggestionS?

  9. xerraire
    Member
    Posted 8 years ago #

    Does this work for links inside the comment put inside the comment box? As that is what I need. Not so much for the commenting author, but for links inside the actual posts.

  10. Lautreamont
    Member
    Posted 8 years ago #

    It's easy. You just need to edit your 'comment-functions.php' inside the 'wp-includes' folder. Search the source code of the file until you find the function code below:


    function get_comment_author_link() {
    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'>$author</a>";
    return apply_filters('get_comment_author_link', $return);
    }

    Now you only need to add target='_blank' to the second 'return' variable as follows:

    $return = "<a href='$url' rel='external nofollow' target='_blank'>$author</a>";

    Save it and upload it to your 'wp-includes' folder. And that's it.

    Hope this solves your problem.

    ¡Saluditos!
    Lautreamont

  11. ptvguy
    Member
    Posted 8 years ago #

    For any of you that may be interested in web accessibility and current standards, the better method for opening a new window is to use accessible JavaScript. The "target" property has long been deprecated.

    An "externalLink" class attribute tied to the anchor tag as outlined by Paul Boag over on boagworld.com is far more accessible and standards compliant. You can set the function up as outlined above, only using class='externalLink' in place of target='_blank'

    The only problem with using this method is that it will force your own user name in comments to open in a new window as well. The workaround for that is to add an extra OR condition to remove the hyperlink from your own user name. The code outlined below should work for you if you're using that type of JavaScript pseudo-class.

    function get_comment_author_link() {
    global $comment;
    $url = get_comment_author_url();
    $author = get_comment_author();

    if ( empty( $url ) || 'http://' == $url || get_bloginfo('name') == $author )
    $return = $author;
    else
    $return = "<a href='$url' rel='external nofollow' title='$author' class='externalLink'>$author</a>";
    return apply_filters('get_comment_author_link', $return);
    }

    If, for some reason, the get_bloginfo('name') isn't excluding your username, then just encase your user name in single quotes in place of that function and remember that it's case-sensitive. In other words, I could accomplish the same thing on my blog by putting 'ptvGuy' in that position.

    On a side note, you could potentially add more OR conditions to that line and exclude other user names from getting a hyperlink as well.

  12. ptvguy
    Member
    Posted 8 years ago #

    Oops, sorry everyone, a slight edit is necessary on the syntax of that last posting. Here's what mine looks like now:

    function get_comment_author_link() {
    global $comment;
    $url = get_comment_author_url();
    $author = get_comment_author();

    if ( empty( $url ) || 'http://' == $url || 'ptvGuy' == $author || 'ptvguy' == $author )
    $return = $author;
    else
    $return = "<a href=\"$url\" class=\"externalLink\" rel=\"external\" title=\"$author\">$author</a>";
    return apply_filters('get_comment_author_link', $return);
    }

    If you don't want your own username to open new windows as well, then replace the parts where I put "ptvGuy" with case-sensitive variations on the spelling of your username. If you don't care about that, then don't edit that part of the original function.

    Also, if you don't want to set up a JavaScript to handle opening external document windows and you'd rather just stick with the old target="_blank", then just replace the part where I put class=\"externalLink\" with target=\"_blank\".

  13. kookyelephant
    Member
    Posted 7 years ago #

    Thanks for this! I wanted the same thing, because my website uses frames. Thanks for the help :)

  14. annevisc
    Member
    Posted 7 years ago #

    Thanks so much, this is great!!! :-)

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags