Using WordPress ‘comment_email’ PHP filter

The comment_email WordPress PHP filter allows you to modify the comment author’s email for display purposes. Be cautious when using this filter to protect the email address from email harvesters.

Usage

add_filter('comment_email', 'your_custom_function', 10, 2);
function your_custom_function($comment_author_email, $comment) {
  // your custom code here
  return $comment_author_email;
}

Parameters

  • $comment_author_email (string) – The comment author’s email address.
  • $comment (WP_Comment) – The comment object.

More information

See WordPress Developer Resources: comment_email

Examples

Obfuscate Email Addresses

Replace the email address with a placeholder to prevent email harvesting.

add_filter('comment_email', 'obfuscate_email_addresses', 10, 2);
function obfuscate_email_addresses($comment_author_email, $comment) {
  return '[email protected]';
}

Append Domain Name

Append your domain name to the comment author’s email address.

add_filter('comment_email', 'append_domain_name', 10, 2);
function append_domain_name($comment_author_email, $comment) {
  return $comment_author_email . '@yourdomain.com';
}

Remove Email TLD

Remove the top-level domain (TLD) from the comment author’s email address.

add_filter('comment_email', 'remove_email_tld', 10, 2);
function remove_email_tld($comment_author_email, $comment) {
  $parts = explode('@', $comment_author_email);
  return $parts[0] . '@';
}

Replace Specific Domain

Replace a specific domain in the comment author’s email address with another domain.

add_filter('comment_email', 'replace_specific_domain', 10, 2);
function replace_specific_domain($comment_author_email, $comment) {
  return str_replace('old-domain.com', 'new-domain.com', $comment_author_email);
}

Uppercase Email Addresses

Convert the comment author’s email address to uppercase.

add_filter('comment_email', 'uppercase_email_addresses', 10, 2);
function uppercase_email_addresses($comment_author_email, $comment) {
  return strtoupper($comment_author_email);
}