get_comment_link( WP_Comment|int|null $comment = null, array $args = array() )

Retrieves the link to a given comment.

Description

See also

Parameters

$comment

(WP_Comment|int|null) (Optional) Comment to retrieve. Default current comment.

Default value: null

$args

(array) (Optional) An array of optional arguments to override the defaults.

  • 'type'
    (string) Passed to get_page_of_comment().
  • 'page'
    (int) Current page of comments, for calculating comment pagination.
  • 'per_page'
    (int) Per-page value for comment pagination.
  • 'max_depth'
    (int) Passed to get_page_of_comment().
  • 'cpage'
    (int|string) Value to use for the comment's "comment-page" or "cpage" value. If provided, this value overrides any value calculated from $page and $per_page.

Default value: array()

Return

(string) The permalink to the given comment.

More Information

Default Arguments

The following default arguments are used unless found in the optional $args argument:

page
The zero-based index for the page where the comment should appear. Defaults to 0. Note: for backward compatibility the entire $args argument is treated as an integer and used for this argument if it is not found to be an array.
type
The type of comment (not used directly). Defaults to 'all'.
per_page
Number of comments per page. Defaults to 0.
max_depth
Maximum depth to be considered for comments, when threaded (not used directly). Defaults to ''

Source

File: wp-includes/comment-template.php

function get_comment_link( $comment = null, $args = array() ) {
	global $wp_rewrite, $in_comment_loop;

	$comment = get_comment( $comment );

	// Back-compat.
	if ( ! is_array( $args ) ) {
		$args = array( 'page' => $args );
	}

	$defaults = array(
		'type'      => 'all',
		'page'      => '',
		'per_page'  => '',
		'max_depth' => '',
		'cpage'     => null,
	);
	$args     = wp_parse_args( $args, $defaults );

	$link = get_permalink( $comment->comment_post_ID );

	// The 'cpage' param takes precedence.
	if ( ! is_null( $args['cpage'] ) ) {
		$cpage = $args['cpage'];

		// No 'cpage' is provided, so we calculate one.
	} else {
		if ( '' === $args['per_page'] && get_option( 'page_comments' ) ) {
			$args['per_page'] = get_option( 'comments_per_page' );
		}

		if ( empty( $args['per_page'] ) ) {
			$args['per_page'] = 0;
			$args['page']     = 0;
		}

		$cpage = $args['page'];

		if ( '' == $cpage ) {
			if ( ! empty( $in_comment_loop ) ) {
				$cpage = get_query_var( 'cpage' );
			} else {
				// Requires a database hit, so we only do it when we can't figure out from context.
				$cpage = get_page_of_comment( $comment->comment_ID, $args );
			}
		}

		/*
		 * If the default page displays the oldest comments, the permalinks for comments on the default page
		 * do not need a 'cpage' query var.
		 */
		if ( 'oldest' === get_option( 'default_comments_page' ) && 1 === $cpage ) {
			$cpage = '';
		}
	}

	if ( $cpage && get_option( 'page_comments' ) ) {
		if ( $wp_rewrite->using_permalinks() ) {
			if ( $cpage ) {
				$link = trailingslashit( $link ) . $wp_rewrite->comments_pagination_base . '-' . $cpage;
			}

			$link = user_trailingslashit( $link, 'comment' );
		} elseif ( $cpage ) {
			$link = add_query_arg( 'cpage', $cpage, $link );
		}
	}

	if ( $wp_rewrite->using_permalinks() ) {
		$link = user_trailingslashit( $link, 'comment' );
	}

	$link = $link . '#comment-' . $comment->comment_ID;

	/**
	 * Filters the returned single comment permalink.
	 *
	 * @since 2.8.0
	 * @since 4.4.0 Added the `$cpage` parameter.
	 *
	 * @see get_page_of_comment()
	 *
	 * @param string     $link    The comment permalink with '#comment-$id' appended.
	 * @param WP_Comment $comment The current comment object.
	 * @param array      $args    An array of arguments to override the defaults.
	 * @param int        $cpage   The calculated 'cpage' value.
	 */
	return apply_filters( 'get_comment_link', $link, $comment, $args, $cpage );
}

Changelog

Version Description
4.4.0 Added the ability for $comment to also accept a WP_Comment object. Added $cpage argument.
1.5.0 Introduced.

© 2003–2021 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/functions/get_comment_link