Categories
General

Linking to a commenter’s author page

Normally, when a logged-in user leaves a comment, a link pointing to the URL in their profile is added to the comment. This is fine, but what if you’ve set up an author template to serve up fancy author pages? Wouldn’t it be better to link there instead? If your theme already has a replacement wp_list_comments function, it’s pretty easy.

Add something like this to the top of your comment callback function in your theme’s functions.php:

$comment_author = get_comment_author();
$comment_author_link = get_comment_author_link();
if (!empty($comment->user_id)){
  if ($author_url = get_author_posts_url($comment->user_id)) {
    $comment_author_link = '<a href="' . $author_url . '">' . $comment_author . '</a>';
  }
}

Now you can echo out $comment_author_linkĀ  wherever you had the standard author link. If the commenter is a logged-in user, the link will point to their fancy author page – otherwise it’ll just point to the commenter’s URL as usual.

Avatar photo

By isaac

I like cats. he/him