* 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_author ) ) {
$user = $comment->user_id ? get_userdata( $comment->user_id ) : false;
$author = $user->display_name;
$author = __( 'Anonymous' );
$author = $comment->comment_author;
* Filters the returned comment author name.
* @since 4.1.0 The `$comment_ID` and `$comment` parameters were added.
* @param string $author The comment author's username.
* @param int $comment_ID The comment ID.
* @param WP_Comment $comment The comment object.
return apply_filters( 'get_comment_author', $author, $comment->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 );
$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 $author The comment author's username.
* @param int $comment_ID The comment ID.
echo apply_filters( 'comment_author', $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 int $comment_ID The comment ID.
* @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 );
$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 $author_email The comment author's email address.
* @param int $comment_ID The comment ID.
echo apply_filters( 'author_email', $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 $linktext 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( $linktext = '', $before = '', $after = '', $comment = null ) {
$link = get_comment_author_email_link( $linktext, $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 $linktext 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( $linktext = '', $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.
$email = apply_filters( 'comment_email', $comment->comment_author_email, $comment );
if ( ( ! empty( $email ) ) && ( '@' !== $email ) ) {
$display = ( '' !== $linktext ) ? $linktext : $email;
$return .= sprintf( '<a href="%1$s">%2$s</a>', esc_url( 'mailto:' . $email ), esc_html( $display ) );
* 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 );
$url = get_comment_author_url( $comment );
$author = get_comment_author( $comment );
if ( empty( $url ) || 'http://' === $url ) {
$return = "<a href='$url' rel='external nofollow ugc' class='url'>$author</a>";
* Filters the comment author's link for display.
* @since 4.1.0 The `$author` and `$comment_ID` parameters were added.
* @param string $return The HTML-formatted comment author link.
* Empty for an invalid URL.
* @param string $author The comment author's username.
* @param int $comment_ID The comment ID.
return apply_filters( 'get_comment_author_link', $return, $author, $comment->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 );
* Retrieve 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.
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.
* @param int $comment_ID The comment ID.
* @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 );
if ( ! empty( $comment ) ) {
$author_url = ( 'http://' === $comment->comment_author_url ) ? '' : $comment->comment_author_url;
$url = esc_url( $author_url, array( 'http', 'https' ) );
$id = $comment->comment_ID;
* Filters the comment author's URL.
* @since 4.1.0 The `$comment_ID` and `$comment` parameters were added.
* @param string $url The comment author's URL.
* @param int $comment_ID The comment ID.
* @param WP_Comment $comment The comment object.
return apply_filters( 'get_comment_author_url', $url, $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 );
$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 $author_url The comment author's URL.
* @param int $comment_ID The comment ID.
echo apply_filters( 'comment_url', $author_url, $comment->comment_ID );
* Retrieves the HTML link of the URL of the author of the current comment.
* $linktext 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 $linktext
* 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 $linktext 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( $linktext = '', $before = '', $after = '', $comment = 0 ) {
$url = get_comment_author_url( $comment );
$display = ( '' !== $linktext ) ? $linktext : $url;
$display = str_replace( 'http://www.', '', $display );
$display = str_replace( 'http://', '', $display );
if ( '/' === substr( $display, -1 ) ) {
$display = substr( $display, 0, -1 );
$return = "$before<a href='$url' rel='external'>$display</a>$after";
* Filters the comment author's returned URL link.
* @param string $return The HTML-formatted comment author URL link.
return apply_filters( 'get_comment_author_url_link', $return );
* Displays the HTML link of the URL of the author of the current comment.
* @since 4.6.0 Added the `$comment` parameter.
* @param string $linktext 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( $linktext = '', $before = '', $after = '', $comment = 0 ) {
echo get_comment_author_url_link( $linktext, $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[] $class Optional. One or more classes to add to the class list.
* @param int|WP_Comment $comment Comment ID or WP_Comment object. Default current comment.
* @param int|WP_Post $post_id Post ID or WP_Post object. Default current post.
* @param bool $echo Optional. Whether to echo or return the output.
* @return void|string Void if `$echo` argument is true, comment classes if `$echo` is false.
function comment_class( $class = '', $comment = null, $post_id = null, $echo = true ) {
// Separates classes with a single space, collates classes for comment DIV.
$class = 'class="' . implode( ' ', get_comment_class( $class, $comment, $post_id ) ) . '"';
* Returns the classes for the comment div as an array.
* @since 4.4.0 Added the ability for `$comment_id` to also accept a WP_Comment object.
* @global int $comment_alt
* @global int $comment_depth
* @global int $comment_thread_alt
* @param string|string[] $class Optional. One or more classes to add to the class list. Default empty.
* @param int|WP_Comment $comment_id Comment ID or WP_Comment object. Default current comment.
* @param int|WP_Post $post_id Post ID or WP_Post object. Default current post.
* @return string[] An array of classes.
function get_comment_class( $class = '', $comment_id = null, $post_id = null ) {
global $comment_alt, $comment_depth, $comment_thread_alt;
$comment = get_comment( $comment_id );
// Get the comment type (comment, trackback).
$classes[] = ( empty( $comment->comment_type ) ) ? 'comment' : $comment->comment_type;
// Add classes for comment authors that are registered users.
$user = $comment->user_id ? get_userdata( $comment->user_id ) : false;
$classes[] = 'comment-author-' . sanitize_html_class( $user->user_nicename, $comment->user_id );
// For comment authors who are the author of the post.
$post = get_post( $post_id );
if ( $comment->user_id === $post->post_author ) {
$classes[] = 'bypostauthor';
if ( empty( $comment_alt ) ) {
if ( empty( $comment_depth ) ) {
if ( empty( $comment_thread_alt ) ) {
if ( $comment_alt % 2 ) {