* Comment template functions
* These functions are meant to live inside of the WordPress loop.
* Retrieves the author of the current comment.
* If the comment has an empty comment_author field, then 'Anonymous' person is
* @since 4.4.0 Added the ability for `$comment_id` to also accept a WP_Comment object.
* @param int|WP_Comment $comment_id Optional. WP_Comment or the ID of the comment for which to retrieve the author.
* Default current comment.
* @return string The comment author
function get_comment_author( $comment_id = 0 ) {
$comment = get_comment( $comment_id );
if ( ! empty( $comment->comment_ID ) ) {
$comment_id = $comment->comment_ID;
} elseif ( is_scalar( $comment_id ) ) {
$comment_id = (string) $comment_id;
if ( empty( $comment->comment_author ) ) {
$user = ! empty( $comment->user_id ) ? get_userdata( $comment->user_id ) : false;
$comment_author = $user->display_name;
$comment_author = __( 'Anonymous' );
$comment_author = $comment->comment_author;
* Filters the returned comment author name.
* @since 4.1.0 The `$comment_id` and `$comment` parameters were added.
* @param string $comment_author The comment author's username.
* @param string $comment_id The comment ID as a numeric string.
* @param WP_Comment $comment The comment object.
return apply_filters( 'get_comment_author', $comment_author, $comment_id, $comment );
* Displays the author of the current comment.
* @since 4.4.0 Added the ability for `$comment_id` to also accept a WP_Comment object.
* @param int|WP_Comment $comment_id Optional. WP_Comment or the ID of the comment for which to print the author.
* Default current comment.
function comment_author( $comment_id = 0 ) {
$comment = get_comment( $comment_id );
$comment_author = get_comment_author( $comment );
* Filters the comment author's name for display.
* @since 4.1.0 The `$comment_id` parameter was added.
* @param string $comment_author The comment author's username.
* @param string $comment_id The comment ID as a numeric string.
echo apply_filters( 'comment_author', $comment_author, $comment->comment_ID );
* Retrieves the email of the author of the current comment.
* @since 4.4.0 Added the ability for `$comment_id` to also accept a WP_Comment object.
* @param int|WP_Comment $comment_id Optional. WP_Comment or the ID of the comment for which to get the author's email.
* Default current comment.
* @return string The current comment author's email
function get_comment_author_email( $comment_id = 0 ) {
$comment = get_comment( $comment_id );
* Filters the comment author's returned email address.
* @since 4.1.0 The `$comment_id` and `$comment` parameters were added.
* @param string $comment_author_email The comment author's email address.
* @param string $comment_id The comment ID as a numeric string.
* @param WP_Comment $comment The comment object.
return apply_filters( 'get_comment_author_email', $comment->comment_author_email, $comment->comment_ID, $comment );
* Displays the email of the author of the current global $comment.
* Care should be taken to protect the email address and assure that email
* harvesters do not capture your commenter's email address. Most assume that
* their email address will not appear in raw form on the site. Doing so will
* enable anyone, including those that people don't want to get the email
* address and use it for their own means good and bad.
* @since 4.4.0 Added the ability for `$comment_id` to also accept a WP_Comment object.
* @param int|WP_Comment $comment_id Optional. WP_Comment or the ID of the comment for which to print the author's email.
* Default current comment.
function comment_author_email( $comment_id = 0 ) {
$comment = get_comment( $comment_id );
$comment_author_email = get_comment_author_email( $comment );
* Filters the comment author's email for display.
* @since 4.1.0 The `$comment_id` parameter was added.
* @param string $comment_author_email The comment author's email address.
* @param string $comment_id The comment ID as a numeric string.
echo apply_filters( 'author_email', $comment_author_email, $comment->comment_ID );
* Displays the HTML email link to the author of the current comment.
* Care should be taken to protect the email address and assure that email
* harvesters do not capture your commenter's email address. Most assume that
* their email address will not appear in raw form on the site. Doing so will
* enable anyone, including those that people don't want to get the email
* address and use it for their own means good and bad.
* @since 4.6.0 Added the `$comment` parameter.
* @param string $link_text Optional. Text to display instead of the comment author's email address.
* @param string $before Optional. Text or HTML to display before the email link. Default empty.
* @param string $after Optional. Text or HTML to display after the email link. Default empty.
* @param int|WP_Comment $comment Optional. Comment ID or WP_Comment object. Default is the current comment.
function comment_author_email_link( $link_text = '', $before = '', $after = '', $comment = null ) {
$link = get_comment_author_email_link( $link_text, $before, $after, $comment );
* Returns the HTML email link to the author of the current comment.
* Care should be taken to protect the email address and assure that email
* harvesters do not capture your commenter's email address. Most assume that
* their email address will not appear in raw form on the site. Doing so will
* enable anyone, including those that people don't want to get the email
* address and use it for their own means good and bad.
* @since 4.6.0 Added the `$comment` parameter.
* @param string $link_text Optional. Text to display instead of the comment author's email address.
* @param string $before Optional. Text or HTML to display before the email link. Default empty.
* @param string $after Optional. Text or HTML to display after the email link. Default empty.
* @param int|WP_Comment $comment Optional. Comment ID or WP_Comment object. Default is the current comment.
* @return string HTML markup for the comment author email link. By default, the email address is obfuscated
* via the {@see 'comment_email'} filter with antispambot().
function get_comment_author_email_link( $link_text = '', $before = '', $after = '', $comment = null ) {
$comment = get_comment( $comment );
* Filters the comment author's email for display.
* Care should be taken to protect the email address and assure that email
* harvesters do not capture your commenter's email address.
* @since 4.1.0 The `$comment` parameter was added.
* @param string $comment_author_email The comment author's email address.
* @param WP_Comment $comment The comment object.
$comment_author_email = apply_filters( 'comment_email', $comment->comment_author_email, $comment );
if ( ( ! empty( $comment_author_email ) ) && ( '@' !== $comment_author_email ) ) {
$display = ( '' !== $link_text ) ? $link_text : $comment_author_email;
$comment_author_email_link = $before . sprintf(
'<a href="%1$s">%2$s</a>',
esc_url( 'mailto:' . $comment_author_email ),
return $comment_author_email_link;
* Retrieves the HTML link to the URL of the author of the current comment.
* Both get_comment_author_url() and get_comment_author() rely on get_comment(),
* which falls back to the global comment variable if the $comment_id argument is empty.
* @since 4.4.0 Added the ability for `$comment_id` to also accept a WP_Comment object.
* @param int|WP_Comment $comment_id Optional. WP_Comment or the ID of the comment for which to get the author's link.
* Default current comment.
* @return string The comment author name or HTML link for author's URL.
function get_comment_author_link( $comment_id = 0 ) {
$comment = get_comment( $comment_id );
if ( ! empty( $comment->comment_ID ) ) {
$comment_id = $comment->comment_ID;
} elseif ( is_scalar( $comment_id ) ) {
$comment_id = (string) $comment_id;
$comment_author_url = get_comment_author_url( $comment );
$comment_author = get_comment_author( $comment );
if ( empty( $comment_author_url ) || 'http://' === $comment_author_url ) {
$comment_author_link = $comment_author;
$rel_parts = array( 'ugc' );
if ( ! wp_is_internal_link( $comment_author_url ) ) {
$rel_parts = array_merge(
array( 'external', 'nofollow' )
* Filters the rel attributes of the comment author's link.
* @param string[] $rel_parts An array of strings representing the rel tags
* which will be joined into the anchor's rel attribute.
* @param WP_Comment $comment The comment object.
$rel_parts = apply_filters( 'comment_author_link_rel', $rel_parts, $comment );
$rel = implode( ' ', $rel_parts );
// Empty space before 'rel' is necessary for later sprintf().
$rel = ! empty( $rel ) ? sprintf( ' rel="%s"', $rel ) : '';
$comment_author_link = sprintf(
'<a href="%1$s" class="url"%2$s>%3$s</a>',
* Filters the comment author's link for display.
* @since 4.1.0 The `$comment_author` and `$comment_id` parameters were added.
* @param string $comment_author_link The HTML-formatted comment author link.
* Empty for an invalid URL.
* @param string $comment_author The comment author's username.
* @param string $comment_id The comment ID as a numeric string.
return apply_filters( 'get_comment_author_link', $comment_author_link, $comment_author, $comment_id );
* Displays the HTML link to the URL of the author of the current comment.
* @since 4.4.0 Added the ability for `$comment_id` to also accept a WP_Comment object.
* @param int|WP_Comment $comment_id Optional. WP_Comment or the ID of the comment for which to print the author's link.
* Default current comment.
function comment_author_link( $comment_id = 0 ) {
echo get_comment_author_link( $comment_id );
* Retrieves the IP address of the author of the current comment.
* @since 4.4.0 Added the ability for `$comment_id` to also accept a WP_Comment object.
* @param int|WP_Comment $comment_id Optional. WP_Comment or the ID of the comment for which to get the author's IP address.
* Default current comment.
* @return string Comment author's IP address, or an empty string if it's not available.
function get_comment_author_IP( $comment_id = 0 ) { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.FunctionNameInvalid
$comment = get_comment( $comment_id );
* Filters the comment author's returned IP address.
* @since 4.1.0 The `$comment_id` and `$comment` parameters were added.
* @param string $comment_author_ip The comment author's IP address, or an empty string if it's not available.
* @param string $comment_id The comment ID as a numeric string.
* @param WP_Comment $comment The comment object.
return apply_filters( 'get_comment_author_IP', $comment->comment_author_IP, $comment->comment_ID, $comment ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.NotLowercase
* Displays the IP address of the author of the current comment.
* @since 4.4.0 Added the ability for `$comment_id` to also accept a WP_Comment object.
* @param int|WP_Comment $comment_id Optional. WP_Comment or the ID of the comment for which to print the author's IP address.
* Default current comment.
function comment_author_IP( $comment_id = 0 ) { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.FunctionNameInvalid
echo esc_html( get_comment_author_IP( $comment_id ) );
* Retrieves the URL of the author of the current comment, not linked.
* @since 4.4.0 Added the ability for `$comment_id` to also accept a WP_Comment object.
* @param int|WP_Comment $comment_id Optional. WP_Comment or the ID of the comment for which to get the author's URL.
* Default current comment.
* @return string Comment author URL, if provided, an empty string otherwise.
function get_comment_author_url( $comment_id = 0 ) {
$comment = get_comment( $comment_id );
$comment_author_url = '';
if ( ! empty( $comment ) ) {
$comment_author_url = ( 'http://' === $comment->comment_author_url ) ? '' : $comment->comment_author_url;
$comment_author_url = esc_url( $comment_author_url, array( 'http', 'https' ) );
$comment_id = $comment->comment_ID;
* Filters the comment author's URL.
* @since 4.1.0 The `$comment_id` and `$comment` parameters were added.
* @param string $comment_author_url The comment author's URL, or an empty string.
* @param string|int $comment_id The comment ID as a numeric string, or 0 if not found.
* @param WP_Comment|null $comment The comment object, or null if not found.
return apply_filters( 'get_comment_author_url', $comment_author_url, $comment_id, $comment );
* Displays the URL of the author of the current comment, not linked.
* @since 4.4.0 Added the ability for `$comment_id` to also accept a WP_Comment object.
* @param int|WP_Comment $comment_id Optional. WP_Comment or the ID of the comment for which to print the author's URL.
* Default current comment.
function comment_author_url( $comment_id = 0 ) {
$comment = get_comment( $comment_id );
$comment_author_url = get_comment_author_url( $comment );
* Filters the comment author's URL for display.
* @since 4.1.0 The `$comment_id` parameter was added.
* @param string $comment_author_url The comment author's URL.
* @param string $comment_id The comment ID as a numeric string.
echo apply_filters( 'comment_url', $comment_author_url, $comment->comment_ID );
* Retrieves the HTML link of the URL of the author of the current comment.
* $link_text parameter is only used if the URL does not exist for the comment
* author. If the URL does exist then the URL will be used and the $link_text
* Encapsulate the HTML link between the $before and $after. So it will appear
* in the order of $before, link, and finally $after.
* @since 4.6.0 Added the `$comment` parameter.
* @param string $link_text Optional. The text to display instead of the comment
* author's email address. Default empty.
* @param string $before Optional. The text or HTML to display before the email link.
* @param string $after Optional. The text or HTML to display after the email link.
* @param int|WP_Comment $comment Optional. Comment ID or WP_Comment object.
* Default is the current comment.
* @return string The HTML link between the $before and $after parameters.
function get_comment_author_url_link( $link_text = '', $before = '', $after = '', $comment = 0 ) {
$comment_author_url = get_comment_author_url( $comment );
$display = ( '' !== $link_text ) ? $link_text : $comment_author_url;
$display = str_replace( 'http://www.', '', $display );
$display = str_replace( 'http://', '', $display );
if ( str_ends_with( $display, '/' ) ) {
$display = substr( $display, 0, -1 );
$comment_author_url_link = $before . sprintf(
'<a href="%1$s" rel="external">%2$s</a>',
* Filters the comment author's returned URL link.
* @param string $comment_author_url_link The HTML-formatted comment author URL link.
return apply_filters( 'get_comment_author_url_link', $comment_author_url_link );
* Displays the HTML link of the URL of the author of the current comment.
* @since 4.6.0 Added the `$comment` parameter.
* @param string $link_text Optional. Text to display instead of the comment author's
* email address. Default empty.
* @param string $before Optional. Text or HTML to display before the email link.
* @param string $after Optional. Text or HTML to display after the email link.
* @param int|WP_Comment $comment Optional. Comment ID or WP_Comment object.
* Default is the current comment.
function comment_author_url_link( $link_text = '', $before = '', $after = '', $comment = 0 ) {
echo get_comment_author_url_link( $link_text, $before, $after, $comment );
* Generates semantic classes for each comment element.
* @since 4.4.0 Added the ability for `$comment` to also accept a WP_Comment object.
* @param string|string[] $css_class Optional. One or more classes to add to the class list.
* @param int|WP_Comment $comment Optional. Comment ID or WP_Comment object. Default current comment.
* @param int|WP_Post $post Optional. Post ID or WP_Post object. Default current post.
* @param bool $display Optional. Whether to print or return the output.
* @return void|string Void if `$display` argument is true, comment classes if `$display` is false.
function comment_class( $css_class = '', $comment = null, $post = null, $display = true ) {
// Separates classes with a single space, collates classes for comment DIV.
$css_class = 'class="' . implode( ' ', get_comment_class( $css_class, $comment, $post ) ) . '"';