Using WordPress ‘previous_comments_link_attributes’ PHP filter

‘previous_comments_link_attributes’ is a WordPress PHP filter that allows you to modify the anchor tag attributes for the previous comments page link.

Usage

add_filter( 'previous_comments_link_attributes', 'your_function_name' );

function your_function_name( $attributes ) {
    // Your code here to modify the attributes
    return $attributes;
}

Parameters

  • $attributes (string)
    • Attributes for the anchor tag.

Examples

add_filter( 'previous_comments_link_attributes', 'add_css_class_to_previous_comments_link' );

function add_css_class_to_previous_comments_link( $attributes ) {
    $attributes .= ' class="custom-class"';
    return $attributes;
}

This code snippet adds the custom-class CSS class to the previous comments link’s anchor tag.

add_filter( 'previous_comments_link_attributes', 'add_title_to_previous_comments_link' );

function add_title_to_previous_comments_link( $attributes ) {
    $attributes .= ' title="Go to previous comments page"';
    return $attributes;
}

This example adds a custom title attribute “Go to previous comments page” to the previous comments link’s anchor tag.

add_filter( 'previous_comments_link_attributes', 'open_previous_comments_link_in_new_tab' );

function open_previous_comments_link_in_new_tab( $attributes ) {
    $attributes .= ' target="_blank"';
    return $attributes;
}

This code snippet adds the target="_blank" attribute to the previous comments link’s anchor tag, making it open in a new browser tab.

add_filter( 'previous_comments_link_attributes', 'add_nofollow_to_previous_comments_link' );

function add_nofollow_to_previous_comments_link( $attributes ) {
    $attributes .= ' rel="nofollow"';
    return $attributes;
}

This example adds the rel="nofollow" attribute to the previous comments link’s anchor tag, instructing search engines not to follow the link.

add_filter( 'previous_comments_link_attributes', 'customize_previous_comments_link' );

function customize_previous_comments_link( $attributes ) {
    $attributes .= ' class="custom-class" title="Go to previous comments page" target="_blank" rel="nofollow"';
    return $attributes;
}

This code snippet combines all the previous examples, adding a custom class, title, target, and rel attribute to the previous comments link’s anchor tag.