get_edit_comment_link()WP 2.3.0

Retrieves the edit comment link.

1 time — 0.0028741 sec (very slow) | 50000 times — 6.57 sec (fast) | PHP 7.3.12, WP 5.3.2
Hooks from the function

Return

String|null. The edit comment link URL for the given comment, or void if the comment id does not exist or the current user is not allowed to edit it.

Usage

get_edit_comment_link( $comment_id, $context );
$comment_id(int|WP_Comment)
Comment ID or WP_Comment object.
$context(string)
Context in which the URL should be used. Either 'display', to include HTML entities, or 'url'.
Default: 'display'

Examples

0

#1 Get the link (URL) to edit the comment

echo get_edit_comment_link( 2020 );
// http://example.com/wp-admin/comment.php?action=editcomment&c=2020
0

#2 Get a full-fledged link

This example is analogous to what the edit_comment_link() function returns:

echo '<a class="comment-edit-link" href="' . get_edit_comment_link( 2020 ) . '">✎</a>';

Changelog

Since 2.3.0 Introduced.
Since 6.7.0 The $context parameter was added.

get_edit_comment_link() code WP 6.7.2

function get_edit_comment_link( $comment_id = 0, $context = 'display' ) {
	$comment = get_comment( $comment_id );

	if ( ! is_object( $comment ) || ! current_user_can( 'edit_comment', $comment->comment_ID ) ) {
		return;
	}

	if ( 'display' === $context ) {
		$action = 'comment.php?action=editcomment&amp;c=';
	} else {
		$action = 'comment.php?action=editcomment&c=';
	}

	$location = admin_url( $action ) . $comment->comment_ID;

	// Ensure the $comment_id variable passed to the filter is always an ID.
	$comment_id = (int) $comment->comment_ID;

	/**
	 * Filters the comment edit link.
	 *
	 * @since 2.3.0
	 * @since 6.7.0 The $comment_id and $context parameters are now being passed to the filter.
	 *
	 * @param string $location   The edit link.
	 * @param int    $comment_id Unique ID of the comment to generate an edit link.
	 * @param string $context    Context to include HTML entities in link. Default 'display'.
	 */
	return apply_filters( 'get_edit_comment_link', $location, $comment_id, $context );
}