Using WordPress ‘cancel_comment_reply_link’ PHP filter

The cancel_comment_reply_link WordPress PHP filter modifies the HTML-formatted cancel comment reply link.

Usage

add_filter('cancel_comment_reply_link', 'your_custom_function', 10, 3);

function your_custom_function($formatted_link, $link, $text) {
    // your custom code here
    return $formatted_link;
}

Parameters

  • $formatted_link (string) – The HTML-formatted cancel comment reply link.
  • $link (string) – Cancel comment reply link URL.
  • $text (string) – Cancel comment reply link text.

More information

See WordPress Developer Resources: cancel_comment_reply_link

Examples

Modify the text displayed for the cancel reply link.

add_filter('cancel_comment_reply_link', 'change_cancel_reply_link_text', 10, 3);

function change_cancel_reply_link_text($formatted_link, $link, $text) {
    $text = "Cancel reply to this comment";
    return '<a href="' . $link . '" id="cancel-comment-reply-link" rel="nofollow">' . $text . '</a>';
}

Add a custom CSS class to the cancel reply link.

add_filter('cancel_comment_reply_link', 'add_css_class_to_cancel_reply_link', 10, 3);

function add_css_class_to_cancel_reply_link($formatted_link, $link, $text) {
    return '<a href="' . $link . '" id="cancel-comment-reply-link" class="my-custom-class" rel="nofollow">' . $text . '</a>';
}

Insert an icon before the cancel reply link text.

add_filter('cancel_comment_reply_link', 'add_icon_to_cancel_reply_link', 10, 3);

function add_icon_to_cancel_reply_link($formatted_link, $link, $text) {
    $icon = '<i class="fas fa-times"></i> ';
    return '<a href="' . $link . '" id="cancel-comment-reply-link" rel="nofollow">' . $icon . $text . '</a>';
}

Modify the URL of the cancel reply link.

add_filter('cancel_comment_reply_link', 'change_cancel_reply_link_url', 10, 3);

function change_cancel_reply_link_url($formatted_link, $link, $text) {
    $custom_url = home_url('/');
    return '<a href="' . $custom_url . '" id="cancel-comment-reply-link" rel="nofollow">' . $text . '</a>';
}

Completely remove the cancel reply link.

add_filter('cancel_comment_reply_link', 'remove_cancel_reply_link', 10, 3);

function remove_cancel_reply_link($formatted_link, $link, $text) {
    return '';
}