comment_reply_link()WP 2.7.0

Displays the HTML content for reply to comment link.

1 time — 0.001392 sec (very slow) | 50000 times — 18.71 sec (slow)

No Hooks.

Return

null. Nothing (null).

Usage

comment_reply_link( $args, $comment, $post );
$args(array)
Override default options.
Default: empty array
$comment(int|WP_Comment)
Comment being replied to.
Default: current comment
$post(int|WP_Post)
Post ID or WP_Post object the comment is going to be displayed on.
Default: current post

Examples

0

#1 Link to reply to comment

Let's display a link to reply to a comment of maximum nesting level - 5, and change the link text to "reply to comment":

<?php comment_reply_link( [ 'reply_text' => "reply to comment", 'depth' => 5 ] ); ?>

Will display a link like this:

<a rel="nofollow" 
	class="comment-reply-link" 
	href="#comment-5296" 
	data-commentid="5296" 
	data-postid="1222" 
	data-belowelement="comment-5296" 
	data-respondelement="respond" 
	aria-label="Comment on Digital post"
>Reply</a>
0

#2 Connect comment-reply.js

By clicking on the link that comment_reply_link() displays, the user should be redirected to the comment response form. But if you connect the script comment-reply.js, then when you click on this link, the comment response form itself should move under the current comment.

To connect the script comment-reply.js, which is in the WP core files, add this line to header.php:

if( is_singular() ){
	wp_enqueue_script('comment-reply');
}

Or better add this code to funcions.php:

add_action( 'wp_enqueue_scripts', 'enqueue_comment_reply' );

function enqueue_comment_reply() {

	if( is_singular() ){
		wp_enqueue_script('comment-reply');
	}
}
0

#3 Connect comment-reply.js only where you need it:

The second example is a bit simpler. This example also connect the script, but before it does, it checks if it's really needed. The "if" condition checks whether it is a posts page (is_singular()), and whether comments are open for that page (comments_open()), and whether the tree comments option is enabled (get_option()). If all of these conditions are not met, then it is logical that this script is not needed, so it will not be enabled:

add_action( 'wp_enqueue_scripts', 'enqueue_comment_reply' );

function enqueue_comment_reply() {

	if( is_singular() && comments_open() && get_option('thread_comments') == 1 ){
		wp_enqueue_script('comment-reply');
	}
}
0

#4 Replace CSS class on reply link using hook

add_filter( 'comment_reply_link', 'wpdocs_comment_reply_link_class' );

function wpdocs_comment_reply_link_class( $class ) {

	$class = str_replace( "class='comment-reply-link", "class='comment-reply-link your-class-name", $class );

	return $class;
}

Notes

Changelog

Since 2.7.0 Introduced.

comment_reply_link() code WP 6.5.2

function comment_reply_link( $args = array(), $comment = null, $post = null ) {
	echo get_comment_reply_link( $args, $comment, $post );
}