WordPress.org

Ready to get started?Download WordPress

Forums

Make commenters urls open in a new window (4 posts)

  1. Mark (podz)
    Support Maven
    Posted 8 years ago #

    Open comment-functions.php, scroll down to line 352, you'll see
    this function:

    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);
    }

    change this line:
    $return = "<a href='$url' rel='external nofollow'>$author</a>";
    to this:
    $return = "<a href='$url' rel='external nofollow'
    target='_blank'>$author</a>";

    Keep a backup of the original, and make a note for upgrading.

  2. ptvguy
    Member
    Posted 8 years ago #

    Podz:

    I've been fighting with this one for awhile and no matter what I do, I can't get it to accept more than two elements through the anchor tag. In other words, if I try to do it like yours, it takes the href and the rel elements and smashes them together into a single href element and adds the target as the second element thus creating a nonworking URL. In my case, I'm also trying to add in a fourth element as a title attribute. (See below.) I've also modded the OR conditions to eliminate my own comments from even having a link.

    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' rel='external' class='externalLink' title='$author'>$author</a>";
    return apply_filters('get_comment_author_link', $return);
    }

    I'm not sure if this is a theme-related problem or if it has to do with the sprintf. BTW, if you're wondering what the point of all that is, it's to add a little accessibility to my comment hyperlinks.

  3. ptvguy
    Member
    Posted 8 years ago #

    Nevermind, podz, I was overthinking it. Now I'm set up to have accessible external links even where the comment author's name links to their site, and I've eliminated my username in my own comments from having any kind of hyperlink:

    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);
    }

    Onward to my next battle, how to do the same thing to external hyperlinks contained within the text of a comment...

  4. ptvguy
    Member
    Posted 8 years ago #

    Oops, almost forgot, if you don't want to set up a JavaScript to handle opening new windows--and I really recommend the one I use from Paul Boag over at boagworld--then it would probably work better as follows:

    Change this line as sited above by podz:

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

    to this:

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

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.