Using WordPress ‘get_post_reply_link()’ PHP function

The get_post_reply_link() WordPress PHP function retrieves the HTML content for a reply to post link.

Usage

get_post_reply_link( $args, $post );

Custom example:

echo get_post_reply_link( array( 'reply_text' => 'Reply to this comment' ), $post_id );

Parameters

  • $args array (optional): Override default arguments.
    • add_below string: The first part of the selector used to identify the comment to respond below. Default is ‘post’.
    • respond_id string: The selector identifying the responding comment. Default ‘respond’.
    • reply_text string: Text of the Reply link. Default is ‘Leave a Comment’.
    • login_text string: Text of the link to reply if logged out. Default is ‘Log in to leave a Comment’.
    • before string: Text or HTML to add before the reply link.
    • after string: Text or HTML to add after the reply link. Default: array()
  • $post int|WP_Post (optional): Post ID or WP_Post object the comment is going to be displayed on. Default current post. Default: null

More information

See WordPress Developer Resources: get_post_reply_link

Examples

This example displays a custom reply link with the text “Reply to this comment” instead of the default text.

echo get_post_reply_link( array( 'reply_text' => 'Reply to this comment' ) );

This example adds custom text before and after the reply link.

echo get_post_reply_link( array( 'before' => '<div class="custom-reply">', 'after' => '</div>' ) );

Change the selector identifying the responding comment

This example changes the respond_id selector to ‘custom-respond’.

echo get_post_reply_link( array( 'respond_id' => 'custom-respond' ) );

This example shows the reply link only to logged-in users.

if ( is_user_logged_in() ) {
    echo get_post_reply_link();
}

This example changes the reply link text for logged-out users to “Please log in to reply”.

echo get_post_reply_link( array( 'login_text' => 'Please log in to reply' ) );