Using WordPress ‘get_comment_author_url’ PHP filter

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

Usage

add_filter('get_comment_author_url', 'your_function_name', 10, 3);
function your_function_name($url, $comment_id, $comment) {
  // your custom code here
  return $url;
}

Parameters

  • $url (string) – The comment author’s URL, or an empty string.
  • $comment_id (string|int) – The comment ID as a numeric string, or 0 if not found.
  • $comment (WP_Comment|null) – The comment object, or null if not found.

More information

See WordPress Developer Resources: get_comment_author_url

Examples

Add “nofollow” attribute to comment author URLs

Prevent search engines from following comment author links by adding the “nofollow” attribute.

add_filter('get_comment_author_url', 'add_nofollow_to_comment_author_url', 10, 3);
function add_nofollow_to_comment_author_url($url, $comment_id, $comment) {
  if (!empty($url)) {
    $url = str_replace('<a ', '<a rel="nofollow" ', $url);
  }
  return $url;
}

Remove “http://” from comment author URLs

Display cleaner URLs by removing “http://” from the comment author’s URL.

add_filter('get_comment_author_url', 'remove_http_from_comment_author_url', 10, 3);
function remove_http_from_comment_author_url($url, $comment_id, $comment) {
  $url = str_replace('http://', '', $url);
  return $url;
}

Add a custom class to comment author URLs

Style comment author links by adding a custom CSS class.

add_filter('get_comment_author_url', 'add_custom_class_to_comment_author_url', 10, 3);
function add_custom_class_to_comment_author_url($url, $comment_id, $comment) {
  if (!empty($url)) {
    $url = str_replace('<a ', '<a class="custom-class" ', $url);
  }
  return $url;
}

Replace comment author URLs with their Gravatar profile URLs

Redirect comment author links to their Gravatar profiles.

add_filter('get_comment_author_url', 'replace_with_gravatar_profile_url', 10, 3);
function replace_with_gravatar_profile_url($url, $comment_id, $comment) {
  if ($comment instanceof WP_Comment) {
    $email = $comment->comment_author_email;
    $hash = md5(strtolower(trim($email)));
    $url = "https://www.gravatar.com/{$hash}";
  }
  return $url;
}

Add UTM parameters to comment author URLs

Track comment author link clicks by appending UTM parameters to their URLs.

add_filter('get_comment_author_url', 'add_utm_parameters_to_comment_author_url', 10, 3);
function add_utm_parameters_to_comment_author_url($url, $comment_id, $comment) {
  if (!empty($url)) {
    $utm_parameters = '?utm_source=website&utm_medium=comment-author-link&utm_campaign=comment-tracking';
    $url .= $utm_parameters;
  }
  return $url;
}