get_comment_reply_link

Retrieve HTML content for reply to comment link.

Description

(void|false|string) get_comment_reply_link( (array) $args = array(), (constant) $comment = null, (null) $post = null ); 

Returns (void|false|string)

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

Parameters (3)

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

Options

  • add_below (string) => 'comment'

    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) => 'Reply'

    The text of the Reply link.

  • login_text (string) => 'Log in to Reply'

    The text of the link to reply if logged out.

  • max_depth (int) => 0

    The max depth of the comment tree.

  • depth (int) => 0

    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.

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 'comment'
     */
    'add_below' => 'comment',

    /**
     * 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',

    /**
     * The text of the Reply link.
     *
     * @type string
     * @default 'Reply'
     */
    'reply_text' => 'Reply',

    /**
     * The text of the link to reply if logged out.
     *
     * @type string
     * @default 'Log in to Reply'
     */
    'login_text' => 'Log in to Reply',

    /**
     * The max depth of the comment tree.
     *
     * @type int
     */
    'max_depth' => 0,

    /**
     * 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.
     *
     * @type int
     */
    'depth' => 0,

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

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

Usage

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

Defined (1)

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

/wp-includes/comment-template.php  
  1. function get_comment_reply_link( $args = array(), $comment = null, $post = null ) { 
  2. $defaults = array( 
  3. 'add_below' => 'comment',  
  4. 'respond_id' => 'respond',  
  5. 'reply_text' => __( 'Reply' ),  
  6. /** translators: Comment reply button text. 1: Comment author name */ 
  7. 'reply_to_text' => __( 'Reply to %s' ),  
  8. 'login_text' => __( 'Log in to Reply' ),  
  9. 'max_depth' => 0,  
  10. 'depth' => 0,  
  11. 'before' => '',  
  12. 'after' => '' 
  13. ); 
  14.  
  15. $args = wp_parse_args( $args, $defaults ); 
  16.  
  17. if ( 0 == $args['depth'] || $args['max_depth'] <= $args['depth'] ) { 
  18. return; 
  19.  
  20. $comment = get_comment( $comment ); 
  21.  
  22. if ( empty( $post ) ) { 
  23. $post = $comment->comment_post_ID; 
  24.  
  25. $post = get_post( $post ); 
  26.  
  27. if ( ! comments_open( $post->ID ) ) { 
  28. return false; 
  29.  
  30. /** 
  31. * Filters the comment reply link arguments. 
  32. * @since 4.1.0 
  33. * @param array $args Comment reply link arguments. See get_comment_reply_link() 
  34. * for more information on accepted arguments. 
  35. * @param WP_Comment $comment The object of the comment being replied to. 
  36. * @param WP_Post $post The WP_Post object. 
  37. */ 
  38. $args = apply_filters( 'comment_reply_link_args', $args, $comment, $post ); 
  39.  
  40. if ( get_option( 'comment_registration' ) && ! is_user_logged_in() ) { 
  41. $link = sprintf( '<a rel="nofollow" class="comment-reply-login" href="%s">%s</a>',  
  42. $args['login_text'] 
  43. ); 
  44. } else { 
  45. $onclick = sprintf( 'return addComment.moveForm( "%1$s-%2$s", "%2$s", "%3$s", "%4$s" )',  
  46. $args['add_below'], $comment->comment_ID, $args['respond_id'], $post->ID 
  47. ); 
  48.  
  49. $link = sprintf( "<a rel='nofollow' class='comment-reply-link' href='%s' onclick='%s' aria-label='%s'>%s</a>",  
  50. esc_url( add_query_arg( 'replytocom', $comment->comment_ID, get_permalink( $post->ID ) ) ) . "#" . $args['respond_id'],  
  51. $onclick,  
  52. esc_attr( sprintf( $args['reply_to_text'], $comment->comment_author ) ),  
  53. $args['reply_text'] 
  54. ); 
  55.  
  56. /** 
  57. * Filters the comment reply link. 
  58. * @since 2.7.0 
  59. * @param string $link The HTML markup for the comment reply link. 
  60. * @param array $args An array of arguments overriding the defaults. 
  61. * @param object $comment The object of the comment being replied. 
  62. * @param WP_Post $post The WP_Post object. 
  63. */ 
  64. return apply_filters( 'comment_reply_link', $args['before'] . $link . $args['after'], $args, $comment, $post );