get_comment_link

Retrieve the link to a given comment.

Description

(string) get_comment_link( (constant) $comment = null, (array) $args = array() ); 

Returns (string)

The permalink to the given comment.

Parameters (2)

0. $comment — Optional. (constant) => null
Comment to retrieve. Default current comment.
1. $args — Optional. (array) => array()
An array of optional arguments to override the defaults.

Options

  • type (string) => ''
  • page (int) => 0

    Current page of comments, for calculating comment pagination.

  • per_page (int) => 0

    Per-page value for comment pagination.

  • max_depth (int) => 0
array(

    /**
     * Passed to get_page_of_comment().
     *
     * @type string
     * @default ''
     */
    'type' => '',

    /**
     * Current page of comments, for calculating comment pagination.
     *
     * @type int
     */
    'page' => 0,

    /**
     * Per-page value for comment pagination.
     *
     * @type int
     */
    'per_page' => 0,

    /**
     * Passed to get_page_of_comment().
     *
     * @type int
     */
    'max_depth' => 0
);        


Usage

  1. if ( !function_exists( 'get_comment_link' ) ) { 
  2. require_once ABSPATH . WPINC . '/comment-template.php'; 
  3.  
  4. // Comment to retrieve. Default current comment. 
  5. $comment = null; 
  6.  
  7. // An array of optional arguments to override the defaults. 
  8. $args = array( 
  9. 'type' => '', 
  10. 'page' => 0, 
  11. 'per_page' => 0, 
  12. 'max_depth' => 0 
  13. ); 
  14.  
  15. // NOTICE! Understand what this does before running. 
  16. $result = get_comment_link($comment, $args); 
  17.  

Defined (1)

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

/wp-includes/comment-template.php  
  1. function get_comment_link( $comment = null, $args = array() ) { 
  2. global $wp_rewrite, $in_comment_loop; 
  3.  
  4. $comment = get_comment($comment); 
  5.  
  6. // Back-compat. 
  7. if ( ! is_array( $args ) ) { 
  8. $args = array( 'page' => $args ); 
  9.  
  10. $defaults = array( 
  11. 'type' => 'all',  
  12. 'page' => '',  
  13. 'per_page' => '',  
  14. 'max_depth' => '',  
  15. 'cpage' => null,  
  16. ); 
  17. $args = wp_parse_args( $args, $defaults ); 
  18.  
  19. $link = get_permalink( $comment->comment_post_ID ); 
  20.  
  21. // The 'cpage' param takes precedence. 
  22. if ( ! is_null( $args['cpage'] ) ) { 
  23. $cpage = $args['cpage']; 
  24.  
  25. // No 'cpage' is provided, so we calculate one. 
  26. } else { 
  27. if ( '' === $args['per_page'] && get_option( 'page_comments' ) ) { 
  28. $args['per_page'] = get_option('comments_per_page'); 
  29.  
  30. if ( empty( $args['per_page'] ) ) { 
  31. $args['per_page'] = 0; 
  32. $args['page'] = 0; 
  33.  
  34. $cpage = $args['page']; 
  35.  
  36. if ( '' == $cpage ) { 
  37. if ( ! empty( $in_comment_loop ) ) { 
  38. $cpage = get_query_var( 'cpage' ); 
  39. } else { 
  40. // Requires a database hit, so we only do it when we can't figure out from context. 
  41. $cpage = get_page_of_comment( $comment->comment_ID, $args ); 
  42.  
  43. /** 
  44. * If the default page displays the oldest comments, the permalinks for comments on the default page 
  45. * do not need a 'cpage' query var. 
  46. */ 
  47. if ( 'oldest' === get_option( 'default_comments_page' ) && 1 === $cpage ) { 
  48. $cpage = ''; 
  49.  
  50. if ( $cpage && get_option( 'page_comments' ) ) { 
  51. if ( $wp_rewrite->using_permalinks() ) { 
  52. if ( $cpage ) { 
  53. $link = trailingslashit( $link ) . $wp_rewrite->comments_pagination_base . '-' . $cpage; 
  54.  
  55. $link = user_trailingslashit( $link, 'comment' ); 
  56. } elseif ( $cpage ) { 
  57. $link = add_query_arg( 'cpage', $cpage, $link ); 
  58.  
  59.  
  60. if ( $wp_rewrite->using_permalinks() ) { 
  61. $link = user_trailingslashit( $link, 'comment' ); 
  62.  
  63. $link = $link . '#comment-' . $comment->comment_ID; 
  64.  
  65. /** 
  66. * Filters the returned single comment permalink. 
  67. * @since 2.8.0 
  68. * @since 4.4.0 Added the `$cpage` parameter. 
  69. * @param string $link The comment permalink with '#comment-$id' appended. 
  70. * @param WP_Comment $comment The current comment object. 
  71. * @param array $args An array of arguments to override the defaults. 
  72. * @param int $cpage The calculated 'cpage' value. 
  73. */ 
  74. return apply_filters( 'get_comment_link', $link, $comment, $args, $cpage );