comment_reply_link()WP 2.7.0

Outputs a link <a> that allows replying to a comment. The tag is used in the comments loop, for each comment.

If JavaScript comment-reply.js is enabled, this template tag will move the comment form under the comment that the user decided to reply to.

The function is intended for use within the comments loop.

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

No Hooks.

Returns

null. Outputs the code for the link.

Usage

<?php comment_reply_link( $args, $comment, $post ); ?>
$args(array)
Various parameters that affect the display of the link.
Default: presets
$comment(integer)
ID of the comment to which we will reply.
Default: null
$post(integer)
ID of the post for which the link is shown.
Default: null

Arguments of the $args parameter

add_below(string)
The name of the prefix for the comment container. By default comment, which means that the form will be moved under the container with the attribute id="comment-23" (23 is the ID of the comment we are replying to).
Default: 'comment'
respond_id(string)
The value of the id attribute of the comment form container. That is, if respond is specified, then when clicking on the link, the block with id="respond" will be moved (this is the comment form block).
Default: 'respond'
reply_text(string)
The text of the link.
Default: __('Reply')
login_text(string)
The text of the link that is shown when registration is required to leave a comment.
Default: __('Log in to Reply')
depth(integer)
The depth of comments for which the link will be shown. For threaded comments. If you specify, for example, 3, then for a comment with a nesting depth of 4, the link will no longer be displayed.
Default: 0
before(string)
Text or HTML to be added before the link.
after(string)
Text or HTML to be added after the link.
Default: array()

Examples

1

#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 7.0

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