Using WordPress ‘get_comment_author_email’ PHP filter

The get_comment_author_email WordPress PHP filter allows you to modify the comment author’s email address before it’s displayed or used in other functions.

Usage

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

Parameters

  • $comment_author_email (string) – The comment author’s email address.
  • $comment_id (string) – The comment ID as a numeric string.
  • $comment (WP_Comment) – The comment object.

More information

See WordPress Developer Resources: get_comment_author_email

Examples

Anonymize Email Addresses

To anonymize email addresses before displaying them, you can use the get_comment_author_email filter.

add_filter('get_comment_author_email', 'anonymize_email', 10, 3);
function anonymize_email($comment_author_email, $comment_id, $comment) {
  // Replace the email address with '[email protected]'
  $comment_author_email = '[email protected]';
  return $comment_author_email;
}

Append Text to Email Addresses

To append a custom text to email addresses before displaying them, you can use the get_comment_author_email filter.

add_filter('get_comment_author_email', 'append_text_to_email', 10, 3);
function append_text_to_email($comment_author_email, $comment_id, $comment) {
  // Append '-example' to the email address
  $comment_author_email .= '-example';
  return $comment_author_email;
}

Remove Domain from Email Addresses

To remove the domain part from email addresses, you can use the get_comment_author_email filter.

add_filter('get_comment_author_email', 'remove_domain_from_email', 10, 3);
function remove_domain_from_email($comment_author_email, $comment_id, $comment) {
  // Remove the domain part from the email address
  $comment_author_email = strstr($comment_author_email, '@', true);
  return $comment_author_email;
}

Change Email Domain

To change the email domain for all comment authors, you can use the get_comment_author_email filter.

add_filter('get_comment_author_email', 'change_email_domain', 10, 3);
function change_email_domain($comment_author_email, $comment_id, $comment) {
  // Change the domain to 'example.com'
  $comment_author_email = preg_replace('/@[^@]+(\.[^@]+)$/', '@example.com', $comment_author_email);
  return $comment_author_email;
}