Using WordPress ‘comment_author_link()’ PHP function

The comment_author_link() WordPress PHP function displays the HTML link to the URL of the author of the current comment.

Usage

This function can be used as follows:

comment_author_link($comment_id);

Here, $comment_id could be an instance of WP_Comment or the ID of the comment. By default, it prints the author’s link for the current comment.

Parameters

  • $comment_id (int|WP_Comment) – Optional. This can be an instance of WP_Comment or the ID of the comment for which to print the author’s link. The default is the current comment.

More information

See WordPress Developer Resources: comment_author_link()

The related function is comment_author_url(), which returns only the comment author’s URL.

Examples

This code will display the author link for the current comment.

comment_author_link();

This code will display the author link for a comment with a specific ID.

comment_author_link(42);

In this case, we first fetch a comment object using get_comment(), then pass it to comment_author_link().

$comment = get_comment(42);
comment_author_link($comment);

This code checks if a specific comment exists before trying to display the author’s link.

$comment = get_comment(42);
if ($comment) {
    comment_author_link($comment);
}

Inside a comments loop, comment_author_link() can be used without any parameters to display the author link for each comment.

while (have_comments()) {
    the_comment();
    comment_author_link();
}

In all these examples, the output will be an HTML anchor tag with the author’s URL and name, like this:

<a href="https://example.com" rel="external nofollow ugc" class="url">Author Name</a>