Using WordPress ‘comments_popup_link_attributes’ PHP filter

The comments_popup_link_attributes WordPress PHP filter allows you to modify the comments link attributes for display.

Usage

add_filter('comments_popup_link_attributes', 'your_custom_function');
function your_custom_function($attributes) {
    // your custom code here
    return $attributes;
}

Parameters

  • $attributes (string) – The comments link attributes. Default empty.

More information

See WordPress Developer Resources: comments_popup_link_attributes

Examples

Add a CSS class named custom-class to the comments link.

add_filter('comments_popup_link_attributes', 'add_custom_class_to_comments_link');
function add_custom_class_to_comments_link($attributes) {
    $attributes .= 'class="custom-class"';
    return $attributes;
}

Add a custom data attribute

Add a custom data attribute named data-custom-attribute with the value example-value.

add_filter('comments_popup_link_attributes', 'add_custom_data_attribute');
function add_custom_data_attribute($attributes) {
    $attributes .= 'data-custom-attribute="example-value"';
    return $attributes;
}

Add an ID attribute named comments-link to the comments link.

add_filter('comments_popup_link_attributes', 'add_id_to_comments_link');
function add_id_to_comments_link($attributes) {
    $attributes .= 'id="comments-link"';
    return $attributes;
}

Add a title attribute

Add a title attribute with the value View Comments to the comments link.

add_filter('comments_popup_link_attributes', 'add_title_attribute');
function add_title_attribute($attributes) {
    $attributes .= 'title="View Comments"';
    return $attributes;
}

Combine multiple attributes

Add a custom CSS class, an ID, and a title attribute to the comments link.

add_filter('comments_popup_link_attributes', 'add_multiple_attributes');
function add_multiple_attributes($attributes) {
    $attributes .= 'class="custom-class" id="comments-link" title="View Comments"';
    return $attributes;
}