Using WordPress ‘get_comment_author_link’ PHP filter

The get_comment_author_link WordPress PHP filter allows you to modify the comment author’s link before it is displayed on your website.

Usage

add_filter('get_comment_author_link', 'your_custom_function', 10, 3);

function your_custom_function($return, $author, $comment_id) {
    // your custom code here
    return $return;
}

Parameters

  • $return (string) – The HTML-formatted comment author link. Empty for an invalid URL.
  • $author (string) – The comment author’s username.
  • $comment_id (string) – The comment ID as a numeric string.

More information

See WordPress Developer Resources: get_comment_author_link

Examples

This example adds a custom CSS class to the comment author’s link.

add_filter('get_comment_author_link', 'add_custom_css_class_to_author_link', 10, 3);

function add_custom_css_class_to_author_link($return, $author, $comment_id) {
    $return = str_replace('<a ', '<a class="custom-class" ', $return);
    return $return;
}

This example adds a title attribute to the comment author’s link.

add_filter('get_comment_author_link', 'add_title_attribute_to_author_link', 10, 3);

function add_title_attribute_to_author_link($return, $author, $comment_id) {
    $title = 'Visit ' . esc_attr($author) . '\'s website';
    $return = str_replace('<a ', '<a title="' . $title . '" ', $return);
    return $return;
}

This example adds a target="_blank" attribute to the author link, which opens it in a new browser tab.

add_filter('get_comment_author_link', 'open_author_links_in_new_tab', 10, 3);

function open_author_links_in_new_tab($return, $author, $comment_id) {
    $return = str_replace('<a ', '<a target="_blank" ', $return);
    return $return;
}

This example adds a rel="nofollow" attribute to the author link, instructing search engines not to follow the link.

add_filter('get_comment_author_link', 'add_nofollow_to_author_link', 10, 3);

function add_nofollow_to_author_link($return, $author, $comment_id) {
    $return = str_replace('<a ', '<a rel="nofollow" ', $return);
    return $return;
}

This example removes the link from the comment author’s name and displays only the author’s name as plain text.

add_filter('get_comment_author_link', 'display_author_name_without_link', 10, 3);

function display_author_name_without_link($return, $author, $comment_id) {
    $author_name = strip_tags($return);
    return $author_name;
}