Home WordPress Comment Functions comment_reply_link() Definition In WordPress, comment_reply_link() prints the Reply link for one comment. When a visitor clicks it, the comment form moves under that comment.
It prints the link. It is used in a comment callback for wp_list_comments(). Threaded comments must be on in Settings → Discussion .
Syntax Merge your own arguments with the ones that WordPress passes to your callback.
comment_reply_link( array_merge( $args, array(
'depth' => $depth,
'max_depth' => $args['max_depth'],
) ) ); Call What it does comment_reply_link( $args )Prints a Reply link using the arguments you give. comment_reply_link( $args, 15 )Prints the Reply link for comment 15. get_comment_reply_link( $args )Returns the link instead of printing it.
Useful keys in $args are reply_text, reply_to_text, login_text, before, after, depth, and max_depth.
Full declaration comment_reply_link( array $args = array(), int|WP_Comment $comment = null, int|WP_Post $post = null ): void Parameters Parameter Description $argsOptional. Array of options. Keys include add_below, respond_id, reply_text, reply_to_text, login_text, max_depth, depth, before, and after. $commentOptional. A comment ID or WP_Comment object. Default is the current comment. $postOptional. A post ID or WP_Post object. Default is the current post.
Return Value Return Value Description void Nothing is returned. The function prints the link. It prints nothing if replies are not allowed or the maximum depth is reached.
WordPress Examples Three short theme examples. Each one has a sample output.
1. In a callback 2. Custom link text 3. Wrap the link Example 1 — In a callback Print the Reply link inside a custom comment callback.
function mytheme_comment( $comment, $args, $depth ) { ?>
<li id="comment-<?php comment_ID(); ?>">
<?php comment_text(); ?>
<?php
comment_reply_link( array_merge( $args, array(
'depth' => $depth,
'max_depth' => $args['max_depth'],
) ) );
?>
<?php } <li id="comment-12">
<p>Thanks, this helped me a lot!</p>
<a rel='nofollow' class='comment-reply-link' href='https://example.com/hello-world/?replytocom=12#respond' data-commentid="12" data-postid="42" data-belowelement="comment-12" data-respondelement="respond" data-replyto="Reply to Jane Doe" aria-label="Reply to Jane Doe">Reply</a> How It Works WordPress passes $args and $depth to your callback. array_merge() adds them to the reply link options.The link carries the comment ID in replytocom. Example 2 — Custom link text Change the text of the Reply link.
<?php
comment_reply_link( array_merge( $args, array(
'reply_text' => 'Answer',
'depth' => $depth,
'max_depth' => $args['max_depth'],
) ) );
?> <a rel='nofollow' class='comment-reply-link' href='https://example.com/hello-world/?replytocom=12#respond' ...>Answer</a> How It Works reply_text sets the visible text.The other attributes do not change. Use reply_to_text to change the screen reader label. Example 3 — Wrap the link Add a wrapper with before and after.
<?php
comment_reply_link( array_merge( $args, array(
'before' => '<div class="reply">',
'after' => '</div>',
'depth' => $depth,
'max_depth' => $args['max_depth'],
) ) );
?> <div class="reply"><a rel='nofollow' class='comment-reply-link' ...>Reply</a></div> How It Works before prints before the link.after prints after the link.If no link is printed, the wrapper is not printed either. Frequently Asked Questions What does comment_reply_link() do? It prints a Reply link for one comment so a visitor can answer it.
Why does the Reply link not show? Threaded comments may be off, the maximum depth may be reached, or you did not pass depth and max_depth.
What if the visitor must log in to reply? WordPress prints a Log in to Reply link instead. You can change it with login_text.
Do I need JavaScript? The link works without it, but the form moves under the comment only when the comment-reply script is loaded.
How do I get the link as a string? Use get_comment_reply_link().
About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
8 people found this page helpful
Helpful Share Copy link Suggestion