Using WordPress ‘comment_reply_link()’ PHP function

The comment_reply_link() WordPress PHP function is used to display the HTML content for a reply-to-comment link. You can override the default options, specify the comment being replied to, and the post ID or WP_Post object that the comment will be displayed on.

Usage

The basic usage of this function would look like:

comment_reply_link(array('reply_text' => __('Reply', 'textdomain'), 'depth' => 2, 'max_depth' => 4));

In this example, the comment_reply_link() function is generating a reply link. The text of the link is set to “Reply”. The depth of threading for the comments is set to 2, and the maximum depth of comments allowed is 4.

Parameters

  • $args (array) – Optional. Used to override the default options. Default is an empty array.
  • $comment (int|WP_Comment) – Optional. The comment being replied to. Default is the current comment.
  • $post (int|WP_Post) – Optional. The post ID or WP_Post object the comment is going to be displayed on. Default is the current post.

More Information

See WordPress Developer Resources: comment_reply_link()

Examples

Changing the reply text

comment_reply_link(array('reply_text' => __('Respond', 'textdomain')));

This changes the reply text to “Respond”.

Setting the depth of comments

comment_reply_link(array('depth' => 3));

This sets the depth of threading for the comments to 3.

Setting the maximum depth of comments

comment_reply_link(array('max_depth' => 5));

This sets the maximum depth of comments to 5.

Specifying the comment being replied to

comment_reply_link(array(), 123);

This generates a reply link for the comment with the ID of 123.

Specifying the post ID

comment_reply_link(array(), null, 456);

This generates a reply link for the comment on the post with the ID of 456.