
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.
comment_author();
comment_author( 15 );
| Call | What 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
comment_author( int|WP_Comment $comment_id = 0 ): void
Parameters
| Parameter | Description |
|---|
$comment_id | Optional. A comment ID or a WP_Comment object. Default 0, which means the current comment. |
Return Value
| Return Value | Description |
|---|
| void | Nothing 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.
function mytheme_comment( $comment, $args, $depth ) { ?>
<li id="comment-<?php comment_ID(); ?>">
<strong><?php comment_author(); ?></strong> says:
<?php comment_text(); ?>
<?php }
<li id="comment-12">
<strong>Jane Doe</strong> says:
<p>Thanks, this helped me a lot!</p>
How It Works
- WordPress calls the function once for each comment.
comment_author() prints the name of that comment's author.- The walker adds the closing
</li> for you.
Example 2 — By comment ID
Print the author of one specific comment.
<p>First comment by <?php comment_author( 15 ); ?></p>
<p>First comment by Jane Doe</p>
How It Works
- The argument
15 is the comment ID. - This works even when you are not in the comment loop.
- If the ID does not exist, nothing useful is printed.
Example 3 — Return and escape
Use get_comment_author() when you need a string.
<?php $name = get_comment_author(); ?>
<span class="author"><?php echo esc_html( $name ); ?></span>
<span class="author">Jane Doe</span>
How It Works
get_comment_author() returns the name.esc_html() makes the name safe to print.- 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.
About the author
Developer, cloud engineer, and technical writer
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