get_comment_reply_link()WP 2.7.0

Retrieve HTML content for reply to comment link.

1 time — 0.000774 sec (slow) | 50000 times — 19.68 sec (slow)

Return

String|false|null. Link to show comment form, if successful. False, if comments are closed.

Usage

get_comment_reply_link( $args, $comment, $post );
$args(array)

Override default arguments.

Default: array()

  • add_below(string)
    The first part of the selector used to identify the comment to respond below. The resulting value is passed as the first parameter to addComment.moveForm(), concatenated as $add_below-$comment->comment_ID.
    Default: 'comment'

  • respond_id(string)
    The selector identifying the responding comment. Passed as the third parameter to addComment.moveForm(), and appended to the link URL as a hash value.
    Default: 'respond'

  • reply_text(string)
    The text of the Reply link.
    Default: 'Reply'

  • login_text(string)
    The text of the link to reply if logged out.
    Default: 'Log in to Reply'

  • max_depth(int)
    The max depth of the comment tree.

  • depth(int)
    The depth of the new comment. Must be greater than 0 and less than the value of the 'thread_comments_depth' option set in Settings > Discussion.

  • before(string)
    The text or HTML to add before the reply link.
    Default: ''

  • after(string)
    The text or HTML to add after the reply link.
    Default: ''
$comment(int|WP_Comment)
Comment being replied to.
Default: current comment
$post(int|WP_Post)
Post ID or WP_Post object the comment is going to be displayed on.
Default: current post

Examples

0

#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":

$link = get_comment_reply_link( [
	'reply_text' => "reply to comment",
	'respond_id' => 'comment',
	'depth'      => 5,
	'max_depth'  => 10,
], 2881, 631 );

// here we can process the link before it is displayed on the screen
//$link = str_replace( 'foo', 'bar', $link );

echo $link;

Outputs this html code:

<a
 rel='nofollow'
 class='comment-reply-link'
 href='/function/comment_reply_link?replytocom=2881#comment'
 onclick='return addComment.moveForm( "comment-2881", "2881", "comment", "631" )'
 aria-label='Comment on Vladimir's post'
>
reply to comment
</a>

Changelog

Since 2.7.0 Introduced.
Since 4.4.0 Added the ability for $comment to also accept a WP_Comment object.

get_comment_reply_link() code WP 6.5.2

function get_comment_reply_link( $args = array(), $comment = null, $post = null ) {
	$defaults = array(
		'add_below'     => 'comment',
		'respond_id'    => 'respond',
		'reply_text'    => __( 'Reply' ),
		/* translators: Comment reply button text. %s: Comment author name. */
		'reply_to_text' => __( 'Reply to %s' ),
		'login_text'    => __( 'Log in to Reply' ),
		'max_depth'     => 0,
		'depth'         => 0,
		'before'        => '',
		'after'         => '',
	);

	$args = wp_parse_args( $args, $defaults );

	if ( 0 == $args['depth'] || $args['max_depth'] <= $args['depth'] ) {
		return;
	}

	$comment = get_comment( $comment );

	if ( empty( $comment ) ) {
		return;
	}

	if ( empty( $post ) ) {
		$post = $comment->comment_post_ID;
	}

	$post = get_post( $post );

	if ( ! comments_open( $post->ID ) ) {
		return false;
	}

	if ( get_option( 'page_comments' ) ) {
		$permalink = str_replace( '#comment-' . $comment->comment_ID, '', get_comment_link( $comment ) );
	} else {
		$permalink = get_permalink( $post->ID );
	}

	/**
	 * Filters the comment reply link arguments.
	 *
	 * @since 4.1.0
	 *
	 * @param array      $args    Comment reply link arguments. See get_comment_reply_link()
	 *                            for more information on accepted arguments.
	 * @param WP_Comment $comment The object of the comment being replied to.
	 * @param WP_Post    $post    The WP_Post object.
	 */
	$args = apply_filters( 'comment_reply_link_args', $args, $comment, $post );

	if ( get_option( 'comment_registration' ) && ! is_user_logged_in() ) {
		$link = sprintf(
			'<a rel="nofollow" class="comment-reply-login" href="%s">%s</a>',
			esc_url( wp_login_url( get_permalink() ) ),
			$args['login_text']
		);
	} else {
		$data_attributes = array(
			'commentid'      => $comment->comment_ID,
			'postid'         => $post->ID,
			'belowelement'   => $args['add_below'] . '-' . $comment->comment_ID,
			'respondelement' => $args['respond_id'],
			'replyto'        => sprintf( $args['reply_to_text'], get_comment_author( $comment ) ),
		);

		$data_attribute_string = '';

		foreach ( $data_attributes as $name => $value ) {
			$data_attribute_string .= " data-{$name}=\"" . esc_attr( $value ) . '"';
		}

		$data_attribute_string = trim( $data_attribute_string );

		$link = sprintf(
			"<a rel='nofollow' class='comment-reply-link' href='%s' %s aria-label='%s'>%s</a>",
			esc_url(
				add_query_arg(
					array(
						'replytocom'      => $comment->comment_ID,
						'unapproved'      => false,
						'moderation-hash' => false,
					),
					$permalink
				)
			) . '#' . $args['respond_id'],
			$data_attribute_string,
			esc_attr( sprintf( $args['reply_to_text'], get_comment_author( $comment ) ) ),
			$args['reply_text']
		);
	}

	$comment_reply_link = $args['before'] . $link . $args['after'];

	/**
	 * Filters the comment reply link.
	 *
	 * @since 2.7.0
	 *
	 * @param string     $comment_reply_link The HTML markup for the comment reply link.
	 * @param array      $args               An array of arguments overriding the defaults.
	 * @param WP_Comment $comment            The object of the comment being replied.
	 * @param WP_Post    $post               The WP_Post object.
	 */
	return apply_filters( 'comment_reply_link', $comment_reply_link, $args, $comment, $post );
}