Using WordPress ‘get_comment_author_url()’ PHP function

The get_comment_author_url() WordPress PHP function retrieves the URL of the author of the current comment, without linking to it.

Usage

$author_url = get_comment_author_url( $comment_id );

Example:

$author_url = get_comment_author_url( 42 );
echo $author_url; // Output: https://example.com

Parameters

  • $comment_id (int|WP_Comment) – Optional. The WP_Comment or the ID of the comment for which to get the author’s URL. Default is the current comment.

More information

See WordPress Developer Resources: get_comment_author_url()

Examples

Display the author URL of the current comment

This example will display the author URL for the current comment in a loop.

if ( have_comments() ) {
  while ( have_comments() ) {
    the_comment();
    $author_url = get_comment_author_url();
    echo 'Author URL: ' . $author_url;
  }
}

Retrieve the author URL using comment ID

This example retrieves the author URL for a specific comment by its ID.

$comment_id = 72;
$author_url = get_comment_author_url( $comment_id );
echo 'Author URL: ' . $author_url;

Display comment author URLs for all comments of a post

This example retrieves the author URLs for all comments of a specific post.

$post_id = 56;
$comments = get_comments( array( 'post_id' => $post_id ) );
foreach ( $comments as $comment ) {
  $author_url = get_comment_author_url( $comment->comment_ID );
  echo 'Author URL: ' . $author_url . '<br>';
}

Add a custom prefix to author URLs

This example adds a custom prefix to the author URLs before displaying them.

$comment_id = 42;
$author_url = get_comment_author_url( $comment_id );
$custom_prefix = 'Author\'s Website: ';
echo $custom_prefix . $author_url;

Display author URLs for top-level comments only

This example retrieves the author URLs for top-level comments of a specific post.

$post_id = 56;
$comments = get_comments( array( 'post_id' => $post_id, 'parent' => 0 ) );
foreach ( $comments as $comment ) {
  $author_url = get_comment_author_url( $comment->comment_ID );
  echo 'Author URL: ' . $author_url . '<br>';
}