Using WordPress ‘comment_author_email()’ PHP function

The comment_author_email() WordPress PHP function is used to display the email of the author of the current global $comment. It’s important to use this function carefully to protect the email address from being harvested and misused.

Usage

This function is typically used when you want to show the author’s email of a comment in your WordPress template. Here’s a custom example:

<a href="mailto:<?php comment_author_email(); ?>"><?php printf( __( 'Contact %s', 'textdomain' ), get_comment_author() ); ?></a>

In this example, the author’s email is embedded into a “mailto:” link, and the link text is “Contact [Author’s Name]”.

Parameters

  • $comment_id int|WP_Comment Optional: This is an optional parameter where you can pass the WP_Comment object or the ID of the comment for which you want to print the author’s email. By default, it targets the current comment.

More information

See WordPress Developer Resources: comment_author_email()
This function is part of the core WordPress functionality and is not deprecated as of the last update.

Examples

This code will create a link on your page that says “Contact [Author’s Name]”. When clicked, it will open a new email in the user’s default email client addressed to the author’s email.

<a href="mailto:<?php comment_author_email(); ?>"><?php printf( __( 'Contact %s', 'textdomain' ), get_comment_author() ); ?></a>

Displaying Email in a Div

This code will display the author’s email inside a div. Make sure to add a layer of protection to prevent email harvesting.

<div class="author-email"><?php comment_author_email(); ?></div>

Using with a Specific Comment ID

If you have a specific comment ID, you can display the author’s email for that comment.

<?php comment_author_email(123); // replace 123 with your comment ID ?>

Displaying in a Sentence

This code creates a sentence using the author’s name and email.

<p><?php printf( __( '%s's email is %s.', 'textdomain' ), get_comment_author(), comment_author_email() ); ?></p>

Displaying with a Custom Text Domain

This code uses a custom text domain for localization.

<a href="mailto:<?php comment_author_email(); ?>"><?php printf( __( 'Contact %s', 'mytextdomain' ), get_comment_author() ); ?></a>