Using WordPress ‘get_comment_author_email()’ PHP function

The get_comment_author_email() WordPress PHP function retrieves the email of the author of the current comment.

Usage

$email = get_comment_author_email( $comment_id );

Example:

$email = get_comment_author_email( 42 );
echo $email; // Outputs: [email protected]

Parameters

  • $comment_id (int|WP_Comment) (optional) – WP_Comment or the ID of the comment for which to get the author’s email. Default current comment.

More information

See WordPress Developer Resources: get_comment_author_email()

Examples

Get the email of the author of the current comment

// Get the email of the author of the current comment
$email = get_comment_author_email();
echo $email; // Outputs: [email protected]

Get the email of the author of a specific comment by ID

// Get the email of the author of the comment with the ID 123
$email = get_comment_author_email( 123 );
echo $email; // Outputs: [email protected]

Get the email of the author of a specific comment using a WP_Comment object

// Get a WP_Comment object for the comment with the ID 456
$comment = get_comment( 456 );

// Get the email of the author of the comment
$email = get_comment_author_email( $comment );
echo $email; // Outputs: [email protected]

Display a message if the author’s email is from a specific domain

// Get the email of the author of the current comment
$email = get_comment_author_email();

// Check if the email is from the "example.com" domain
if ( strpos( $email, "@example.com" ) !== false ) {
    echo "The comment author is from example.com";
} else {
    echo "The comment author is not from example.com";
}

Send an email notification to the author of a comment when it is approved

function notify_comment_author_on_approval( $comment_ID, $comment_approved ) {
    if ( 1 === $comment_approved ) {
        $email = get_comment_author_email( $comment_ID );
        $subject = "Your comment has been approved!";
        $message = "Hi, your comment on our website has been approved. Thanks for your contribution!";
        wp_mail( $email, $subject, $message );
    }
}
add_action( 'wp_set_comment_status', 'notify_comment_author_on_approval', 10, 2 );