The comment_author_url()
WordPress PHP function displays the URL of the author of the current comment. It’s not linked to the author’s name but can be used to create such a link.
Usage
To use the comment_author_url()
, simply call it within your WordPress theme. You may optionally pass a comment ID if you’re not working within the comment loop.
comment_author_url($comment_id);
Parameters
- $comment_id (int|WP_Comment, Optional) – Accepts either a
WP_Comment
object or an integer representing the ID of the comment for which to print the author’s URL. The default is the current comment.
More information
See WordPress Developer Resources: comment_author_url()
This function has been a part of WordPress since its early versions and there’s no deprecation in sight. The source code can be found in wp-includes/comment-template.php
.
Examples
Display Comment Author URL
This code will display the URL of the author of a specific comment, given that you have the ID of the comment. It directly calls the function with the comment ID as a parameter.
// Assume $comment_id is the ID of the comment comment_author_url($comment_id);
Link Comment Author’s Name to Their URL
This code will create a link with the author’s name as the text and the author’s URL as the href. It uses comment_author()
to get the author’s name and comment_author_url()
to get the URL.
echo '<a href="' . comment_author_url() . '">' . comment_author() . '</a>';
Check if Comment Author Has a URL
This code checks if a comment author has a URL. It does so by comparing the output of get_comment_author()
and get_comment_author_link()
. If they are the same, the author doesn’t have a URL.
if (get_comment_author() == get_comment_author_link()) { // The author doesn't have a URL }
Display All Comment Authors’ URLs in a Post
This code will loop through all the comments in a post and display the authors’ URLs. It uses get_comments()
to get all the comments, then loops through them, using comment_author_url()
to print each author’s URL.
$comments = get_comments(array('post_id' => $post->ID)); foreach ($comments as $comment) { comment_author_url($comment->comment_ID); }
Display Comment Author URL in a Custom Comments Callback
This code shows how you might use comment_author_url()
within a custom comments callback function. The callback function is passed to wp_list_comments()
to customize the output of comments.
function my_custom_comments($comment, $args, $depth) { echo comment_author_url($comment->comment_ID); } wp_list_comments(array('callback' => 'my_custom_comments'), $comments);