Home WordPress Comment Functions cancel_comment_reply_link() Definition In WordPress, cancel_comment_reply_link() prints the Cancel reply link. Visitors click it to stop replying to one comment and go back to the normal comment form.
It prints the link. It is used with threaded (nested) comments, next to the form title. If you use comment_form(), WordPress already adds this link for you.
Syntax Call it with no text for the default label, or pass your own text.
cancel_comment_reply_link();
cancel_comment_reply_link( 'Cancel reply' ); Call What it does cancel_comment_reply_link()Prints the link with the default text. cancel_comment_reply_link( 'Cancel' )Prints the link with your own text. get_cancel_comment_reply_link()Returns the link as a string instead of printing it.
The link is hidden until a visitor clicks a Reply link. The comment-reply script must be loaded for this to work.
Full declaration cancel_comment_reply_link( string $link_text = '' ): void Parameters Parameter Description $link_textOptional. The text of the link. If empty, WordPress uses Click here to cancel reply.
Return Value Return Value Description void Nothing is returned. The function prints one a tag.
WordPress Examples Three short theme examples. Each one has a sample output.
1. Default link 2. Custom text 3. Next to the form title Example 1 — Default link Print the cancel link with the default text.
<p class="cancel-wrap"><?php cancel_comment_reply_link(); ?></p> <p class="cancel-wrap"><a rel="nofollow" id="cancel-comment-reply-link" href="/hello-world/#respond" style="display:none;">Click here to cancel reply.</a></p> How It Works WordPress builds one link with the id cancel-comment-reply-link. The link has display:none until the visitor clicks Reply. The link points to the comment form (#respond). Example 2 — Custom text Use a short label of your own.
<?php cancel_comment_reply_link( 'Cancel' ); ?> <a rel="nofollow" id="cancel-comment-reply-link" href="/hello-world/#respond" style="display:none;">Cancel</a> How It Works The first argument is the link text. Only the text changes. The id and the link stay the same. Wrap the text in __() if your theme is translated. Example 3 — Next to the form title Show the form title and the cancel link together.
<h3 id="reply-title">
<?php comment_form_title( 'Leave a comment', 'Reply to %s' ); ?>
<small><?php cancel_comment_reply_link( 'Cancel reply' ); ?></small>
</h3> How It Works comment_form_title() prints the heading text.cancel_comment_reply_link() sits inside small next to it.This is the classic layout of an older comments.php file. Frequently Asked Questions What does cancel_comment_reply_link() do? It prints a link that lets a visitor cancel a reply and return to the main comment form.
Why is the cancel link not visible? It is hidden by default. It shows only after a visitor clicks a Reply link and the comment-reply script runs.
Do I need it if I use comment_form()? No. comment_form() already prints the cancel link in its title area.
How do I get the link as a string? Use get_cancel_comment_reply_link(). It returns the same HTML instead of printing it.
How do I enable the reply script? Add wp_enqueue_script( 'comment-reply' ) in your theme when threaded comments are on and the post is single.
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