Redirect WordPress Commenter Links and Open Them in a New Window

Redirect WordPress Commenter Links and Open Them in a New Window

1. Redirecting Commenter Links

Careful readers may have noticed that on some WordPress blogs, if you open any post and hover the mouse over a commenter’s name, the browser status bar may show an address in this form:

http://earn.yesmall.biz/?r=http://earnfs.sinaapp.com/

After clicking it, the browser still redirects to the commenter’s website. If you look at the comment links on your own site, WordPress normally displays the exact URL entered by the commenter. Both methods eventually open the same destination page, but from the perspective of the site’s link structure, the default form turns every comment into an external link, which may dilute the site’s authority. Changing it to an internal redirect URL makes the effect easier to control.

The method is to add the following code to the current theme’s functions.php. It is more recommended to put it in a child theme or a custom plugin, so it will not be overwritten when the theme is updated.

// Redirect commenter links.
add_filter('get_comment_author_link', 'add_redirect_comment_link', 5);
add_filter('comment_text', 'add_redirect_comment_link', 99);

function add_redirect_comment_link($text = '') {
    $home = home_url('/');
    $text = str_replace('href="', 'href="' . esc_url($home) . '?r=', $text);
    $text = str_replace("href='", "href='" . esc_url($home) . '?r=', $text);
    return $text;
}

add_action('init', 'redirect_comment_link');

function redirect_comment_link() {
    if (empty($_GET['r'])) {
        return;
    }

    $redirect = esc_url_raw(wp_unslash($_GET['r']));

    if (!$redirect) {
        wp_redirect(home_url('/'));
        exit;
    }

    if (!empty($_SERVER['HTTP_REFERER']) && strpos($_SERVER['HTTP_REFERER'], home_url()) !== false) {
        wp_redirect($redirect);
        exit;
    }

    wp_redirect(home_url('/'));
    exit;
}

If you have special requirements, you can also use the same idea to handle URL links in the comment body. However, links in comment text are more numerous and come from more varied sources, so before modifying them it is best to check the output in a test environment to avoid accidentally changing internal links or links generated by the admin area.

2. Opening Comment Links in a New Window

By default, WordPress opens commenter links in the current window. After visitors click a commenter link, they leave your site, and traffic is often lost at that point.

The old method was to directly modify wp-includes/comment-template.php and add target="_blank" after rel="external nofollow" in the following code:

if (empty($url) || 'http://' == $url) {
    $return = $author;
} else {
    $return = "<a rel="external nofollow" href="$url">$author</a>";
}

return apply_filters('get_comment_author_link', $return);

However, directly editing WordPress core files is not recommended, because these changes will be overwritten when WordPress is upgraded. The safer approach is still to handle it through a filter in the theme’s functions.php or in a custom plugin:

add_filter('get_comment_author_link', 'comment_author_link_new_window', 20);

function comment_author_link_new_window($link) {
    if (empty($link)) {
        return $link;
    }

    $link = str_replace('<a ', '<a target="_blank" ', $link);

    if (strpos($link, 'rel=') === false) {
        $link = str_replace('<a ', '<a rel="external nofollow noopener" ', $link);
    } else {
        $link = str_replace('rel="external nofollow"', 'rel="external nofollow noopener"', $link);
        $link = str_replace("rel='external nofollow'", "rel='external nofollow noopener'", $link);
    }

    return $link;
}

Finally, it is recommended to add one line to robots.txt to tell search engines not to crawl these redirect URLs:

Disallow: /?r=*

After saving, refresh the page, check whether the commenter links have changed into internal redirect URLs, and confirm that clicking them opens the target website in a new window.

Leave a Reply