Using WordPress ‘pre_comment_author_email’ PHP filter

pre_comment_author_email is a WordPress PHP filter that allows you to modify the comment author’s email cookie before it is set.

Usage

add_filter('pre_comment_author_email', 'your_custom_function');
function your_custom_function($author_email_cookie) {
    // your custom code here
    return $author_email_cookie;
}

Parameters

  • $author_email_cookie (string) – The comment author’s email cookie.

More information

See WordPress Developer Resources: pre_comment_author_email

Examples

Change email domain

Change the email domain of the comment author’s email to a custom domain.

add_filter('pre_comment_author_email', 'change_email_domain');
function change_email_domain($author_email_cookie) {
    $email_parts = explode('@', $author_email_cookie);
    $email_parts[1] = 'customdomain.com';

    return implode('@', $email_parts);
}

Anonymize email

Anonymize the comment author’s email address by replacing the username with “anonymous”.

add_filter('pre_comment_author_email', 'anonymize_email');
function anonymize_email($author_email_cookie) {
    $email_parts = explode('@', $author_email_cookie);
    $email_parts[0] = 'anonymous';

    return implode('@', $email_parts);
}

Add prefix to email

Add a prefix to the comment author’s email address.

add_filter('pre_comment_author_email', 'add_email_prefix');
function add_email_prefix($author_email_cookie) {
    $prefix = 'prefix_';
    return $prefix . $author_email_cookie;
}

Convert email to lowercase

Convert the comment author’s email address to lowercase.

add_filter('pre_comment_author_email', 'convert_email_to_lowercase');
function convert_email_to_lowercase($author_email_cookie) {
    return strtolower($author_email_cookie);
}

Remove subdomain from email

Remove the subdomain from the comment author’s email address.

add_filter('pre_comment_author_email', 'remove_email_subdomain');
function remove_email_subdomain($author_email_cookie) {
    $email_parts = explode('@', $author_email_cookie);
    $domain_parts = explode('.', $email_parts[1]);
    $domain = end($domain_parts);
    return $email_parts[0] . '@' . $domain;
}