Using WordPress ‘comment_link’ PHP filter

The comment_link WordPress PHP filter is used to modify the current comment’s permalink.

Usage

add_filter('comment_link', 'my_custom_function', 10, 1);

function my_custom_function($comment_permalink) {
    // your custom code here
    return $comment_permalink;
}

Parameters

  • $comment_permalink (string): The current comment permalink.

More information

See WordPress Developer Resources: comment_link

Examples

Add a custom query parameter to the comment permalink for tracking purposes.

function add_tracking_parameter($comment_permalink) {
    $comment_permalink .= '&utm_source=my_custom_source';
    return $comment_permalink;
}
add_filter('comment_link', 'add_tracking_parameter');

Add a custom CSS class to the comment permalink to style it differently.

function add_css_class($comment_permalink) {
    return '<a class="my-custom-class" href="' . $comment_permalink . '">Comment Link</a>';
}
add_filter('comment_link', 'add_css_class');

Add the comment author’s name to the permalink for better search engine optimization.

function include_author_name($comment_permalink) {
    $comment = get_comment();
    $author_name = $comment->comment_author;
    $comment_permalink .= '?author=' . urlencode($author_name);
    return $comment_permalink;
}
add_filter('comment_link', 'include_author_name');

Change the domain of comment permalinks to point to a different website.

function redirect_comment_links($comment_permalink) {
    return str_replace('https://example.com', 'https://new-domain.com', $comment_permalink);
}
add_filter('comment_link', 'redirect_comment_links');

Add the comment’s timestamp to the permalink for better organization and searchability.

function add_comment_timestamp($comment_permalink) {
    $comment = get_comment();
    $timestamp = strtotime($comment->comment_date);
    $comment_permalink .= '?time=' . $timestamp;
    return $comment_permalink;
}
add_filter('comment_link', 'add_comment_timestamp');