The get_comment_link WordPress PHP filter allows you to modify the permalink of a single comment.
Usage
add_filter('get_comment_link', 'your_custom_function', 10, 4); function your_custom_function($link, $comment, $args, $cpage) { // your custom code here return $link; }
Parameters
- $link (string) – The comment permalink with ‘#comment-$id’ appended.
- $comment (WP_Comment) – The current comment object.
- $args (array) – An array of arguments to override the defaults.
- $cpage (int) – The calculated ‘cpage’ value.
More information
See WordPress Developer Resources: get_comment_link
Examples
Add a custom query parameter to the comment link
Add a custom query parameter “my_var” with the value “123” to the comment link.
add_filter('get_comment_link', 'add_custom_query_parameter', 10, 4); function add_custom_query_parameter($link, $comment, $args, $cpage) { $link = add_query_arg('my_var', '123', $link); return $link; }
Remove the comment ID hash from the link
Remove the ‘#comment-$id’ part from the comment link.
add_filter('get_comment_link', 'remove_comment_id_hash', 10, 4); function remove_comment_id_hash($link, $comment, $args, $cpage) { $link = strtok($link, '#'); return $link; }
Add a custom class to the comment link
Add a custom class “my-custom-class” to the comment link.
add_filter('get_comment_link', 'add_custom_class_to_comment_link', 10, 4); function add_custom_class_to_comment_link($link, $comment, $args, $cpage) { $link = preg_replace('/(<a\b[^><]*)>/i', '$1 class="my-custom-class">', $link); return $link; }
Change the comment link to a custom URL
Change the comment link to point to a custom URL based on the comment ID.
add_filter('get_comment_link', 'change_comment_link_to_custom_url', 10, 4); function change_comment_link_to_custom_url($link, $comment, $args, $cpage) { $custom_url = 'https://example.com/comment-' . $comment->comment_ID; return $custom_url; }
Add a timestamp to the comment link
Append the comment timestamp to the comment link as a query parameter.
add_filter('get_comment_link', 'add_timestamp_to_comment_link', 10, 4); function add_timestamp_to_comment_link($link, $comment, $args, $cpage) { $timestamp = strtotime($comment->comment_date); $link = add_query_arg('timestamp', $timestamp, $link); return $link; }