WordPress comment_author() Function

Beginner
4 min read
Updated: Nov 2026
3 examples
Template tag

Definition

In WordPress, comment_author() prints the name of the person who wrote a comment. It is one of the most common comment tags.

It prints the name as plain text. Use it inside the comment loop, for example in a wp_list_comments() callback.

Syntax

Call it with no argument inside the comment loop, or pass a comment ID.

WordPress
comment_author();
comment_author( 15 );
CallWhat it does
comment_author()Prints the name of the current comment author.
comment_author( 15 )Prints the name of the author of comment 15.
get_comment_author()Returns the name instead of printing it.

If the commenter left no name, WordPress prints Anonymous.

Full declaration
WordPress
comment_author( int|WP_Comment $comment_id = 0 ): void

Parameters

ParameterDescription
$comment_idOptional. A comment ID or a WP_Comment object. Default 0, which means the current comment.

Return Value

Return ValueDescription
voidNothing is returned. The function prints the author name.

WordPress Examples

Three short theme examples. Each one has a sample output.

Example 1 — In a callback

Show the author name in a custom comment callback.

WordPress
function mytheme_comment( $comment, $args, $depth ) { ?>
  <li id="comment-<?php comment_ID(); ?>">
    <strong><?php comment_author(); ?></strong> says:
    <?php comment_text(); ?>
<?php }

How It Works

  1. WordPress calls the function once for each comment.
  2. comment_author() prints the name of that comment's author.
  3. The walker adds the closing </li> for you.

Example 2 — By comment ID

Print the author of one specific comment.

WordPress
<p>First comment by <?php comment_author( 15 ); ?></p>

How It Works

  1. The argument 15 is the comment ID.
  2. This works even when you are not in the comment loop.
  3. If the ID does not exist, nothing useful is printed.

Example 3 — Return and escape

Use get_comment_author() when you need a string.

WordPress
<?php $name = get_comment_author(); ?>
<span class="author"><?php echo esc_html( $name ); ?></span>

How It Works

  1. get_comment_author() returns the name.
  2. esc_html() makes the name safe to print.
  3. Use this style when you want to change the name before printing.

Frequently Asked Questions

It prints the name of the author of the current comment.
WordPress prints Anonymous.
Use get_comment_author(). It returns the name as a string.
No. Use comment_author_link() for a linked name.
Yes, if you pass a comment ID. Without an ID it needs a current comment.

Next: comment_author_email()

Print the email address of the current comment author.

comment_author_email() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful