Using WordPress ‘get_comments_pagenum_link’ PHP filter

The get_comments_pagenum_link WordPress PHP filter allows you to modify the comments page number link for the current request.

Usage

add_filter('get_comments_pagenum_link', 'your_custom_function', 10, 1);

function your_custom_function($result) {
    // your custom code here
    return $result;
}

Parameters

  • $result (string): The comments page number link to be filtered.

More information

See WordPress Developer Resources: get_comments_pagenum_link

Examples

This example adds a custom query parameter example_param to the comments page link.

add_filter('get_comments_pagenum_link', 'add_custom_param_to_comments_link', 10, 1);

function add_custom_param_to_comments_link($result) {
    $result = add_query_arg('example_param', 'value', $result);
    return $result;
}

This example adds an anchor #comments-section to the comments page link.

add_filter('get_comments_pagenum_link', 'add_anchor_to_comments_link', 10, 1);

function add_anchor_to_comments_link($result) {
    $result .= '#comments-section';
    return $result;
}

This example changes the comments page link to use a custom permalink structure.

add_filter('get_comments_pagenum_link', 'change_comments_permalink_structure', 10, 1);

function change_comments_permalink_structure($result) {
    $result = str_replace('comment-page-', 'custom-comment-page-', $result);
    return $result;
}

This example adds a custom CSS class custom-class to the comments page link.

add_filter('get_comments_pagenum_link', 'add_custom_class_to_comments_link', 10, 1);

function add_custom_class_to_comments_link($result) {
    $result = str_replace('<a href=', '<a class="custom-class" href=', $result);
    return $result;
}

This example redirects comments page links to a custom URL.

add_filter('get_comments_pagenum_link', 'redirect_comments_links_to_custom_url', 10, 1);

function redirect_comments_links_to_custom_url($result) {
    $result = 'https://www.example.com/custom-comments-page-url/';
    return $result;
}