get_post_reply_link

Retrieve HTML content for reply to post link.

Description

(false|null|string) get_post_reply_link( (array) $args = array(), (constant) $post = null ); 

Returns (false|null|string)

Link to show comment form, if successful. False, if comments are closed.

Parameters (2)

0. $args — Optional. (array) => array()
Override default arguments.

Options

  • add_below (string) => 'post'

    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.

  • respond_id (string) => 'respond'

    The selector identifying the responding comment. Passed as the third parameter to addComment.moveForm(), and appended to the link URL as a hash value.

  • reply_text (string) => 'Leave a Comment'

    Text of the Reply link.

  • login_text (string) => 'Log in to leave a Comment'

    Text of the link to reply if logged out.

  • before (string) => ''

    Text or HTML to add before the reply link.

array(

    /**
     * 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.
     *
     * @type string
     * @default 'post'
     */
    'add_below' => 'post',

    /**
     * The selector identifying the responding comment. Passed as the third parameter to
     * addComment.moveForm(), and appended to the link URL as a hash value.
     *
     * @type string
     * @default 'respond'
     */
    'respond_id' => 'respond',

    /**
     * Text of the Reply link.
     *
     * @type string
     * @default 'Leave a Comment'
     */
    'reply_text' => 'Leave a Comment',

    /**
     * Text of the link to reply if logged out.
     *
     * @type string
     * @default 'Log in to leave a Comment'
     */
    'login_text' => 'Log in to leave a Comment',

    /**
     * Text or HTML to add before the reply link.
     *
     * @type string
     * @default ''
     */
    'before' => ''
);        

1. $post — Optional. (constant) => null
Post ID or WP_Post object the comment is going to be displayed on. Default current post.

Usage

  1. if ( !function_exists( 'get_post_reply_link' ) ) { 
  2. require_once ABSPATH . WPINC . '/comment-template.php'; 
  3.  
  4. // Optional. Override default arguments. 
  5. $args = array( 
  6. 'add_below' => 'post', 
  7. 'respond_id' => 'respond', 
  8. 'reply_text' => 'Leave a Comment', 
  9. 'login_text' => 'Log in to leave a Comment', 
  10. 'before' => '' 
  11. ); 
  12.  
  13. // Optional. Post ID or WP_Post object the comment is going to be displayed on. 
  14. // Default current post. 
  15. $post = null; 
  16.  
  17. // NOTICE! Understand what this does before running. 
  18. $result = get_post_reply_link($args, $post); 
  19.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/comment-template.php  
  1. function get_post_reply_link($args = array(), $post = null) { 
  2. $defaults = array( 
  3. 'add_below' => 'post',  
  4. 'respond_id' => 'respond',  
  5. 'reply_text' => __('Leave a Comment'),  
  6. 'login_text' => __('Log in to leave a Comment'),  
  7. 'before' => '',  
  8. 'after' => '',  
  9. ); 
  10.  
  11. $args = wp_parse_args($args, $defaults); 
  12.  
  13. $post = get_post($post); 
  14.  
  15. if ( ! comments_open( $post->ID ) ) { 
  16. return false; 
  17.  
  18. if ( get_option('comment_registration') && ! is_user_logged_in() ) { 
  19. $link = sprintf( '<a rel="nofollow" class="comment-reply-login" href="%s">%s</a>',  
  20. $args['login_text'] 
  21. ); 
  22. } else { 
  23. $onclick = sprintf( 'return addComment.moveForm( "%1$s-%2$s", "0", "%3$s", "%2$s" )',  
  24. $args['add_below'], $post->ID, $args['respond_id'] 
  25. ); 
  26.  
  27. $link = sprintf( "<a rel='nofollow' class='comment-reply-link' href='%s' onclick='%s'>%s</a>",  
  28. get_permalink( $post->ID ) . '#' . $args['respond_id'],  
  29. $onclick,  
  30. $args['reply_text'] 
  31. ); 
  32. $formatted_link = $args['before'] . $link . $args['after']; 
  33.  
  34. /** 
  35. * Filters the formatted post comments link HTML. 
  36. * @since 2.7.0 
  37. * @param string $formatted The HTML-formatted post comments link. 
  38. * @param int|WP_Post $post The post ID or WP_Post object. 
  39. */ 
  40. return apply_filters( 'post_comments_link', $formatted_link, $post );