Using WordPress ‘get_edit_comment_link()’ PHP function

The get_edit_comment_link() WordPress PHP function retrieves the edit comment link.

Usage

$edit_comment_link = get_edit_comment_link( $comment_id );

Parameters

  • $comment_id (int|WP_Comment, Optional) – Comment ID or WP_Comment object.

More information

See WordPress Developer Resources: get_edit_comment_link

Examples

This example retrieves the edit comment link using a comment ID.

$comment_id = 15;
$edit_comment_link = get_edit_comment_link( $comment_id );
echo $edit_comment_link;

This example retrieves the edit comment link using a WP_Comment object.

$comment = get_comment( 15 );
$edit_comment_link = get_edit_comment_link( $comment );
echo $edit_comment_link;

This example displays the edit comment link with the text “Edit this comment”.

$comment_id = 15;
$edit_comment_link = get_edit_comment_link( $comment_id );
echo '<a href="' . esc_url( $edit_comment_link ) . '">Edit this comment</a>';

This example shows how to display the edit comment link for each comment in a list of comments.

$comments = get_comments();
foreach ( $comments as $comment ) {
    $edit_comment_link = get_edit_comment_link( $comment );
    echo '<a href="' . esc_url( $edit_comment_link ) . '">Edit comment #' . $comment->comment_ID . '</a><br>';
}

This example displays the edit comment link only if the user is logged in and has the ‘edit_comment’ capability.

$comment_id = 15;
if ( current_user_can( 'edit_comment', $comment_id ) ) {
    $edit_comment_link = get_edit_comment_link( $comment_id );
    echo '<a href="' . esc_url( $edit_comment_link ) . '">Edit this comment</a>';
}