How to change title attribute returned by comments_popup_link()?

I can change label of comments by altering Comment(s) from parameter of following function: comments_popup_link('No Comments;', '1 Comment;', '% Comments;');
But, seems that title attribute is returned from core modules.
Suggest me to alter title attribute without editing core modules.

Related posts

Leave a Reply

2 comments

  1. If you look into the function comments_popup_link() you will see the following code at the end:

    $title = the_title_attribute( array('echo' => 0 ) );
    
    echo apply_filters( 'comments_popup_link_attributes', '' );
    
    echo ' title="' . esc_attr( sprintf( __('Comment on %s'), $title ) ) . '">';
    comments_number( $zero, $one, $more );
    echo '</a>'; // last line
    

    Note the call to __(), the translation function. We can filter 'gettext' to change the result. Since we do not want to run our filter on every translation – that would be too slow – we start the filter when the hook 'comments_popup_link_attributes' is called:

    add_filter( 'comments_popup_link_attributes', 't5_cclta_init' );
    
    function t5_cclta_init( $attrs )
    {
        add_filter( 'gettext', 't5_cclta_change_title', 10, 3 );
        return $attrs;
    }
    

    Now we need just the real filter function:

    function t5_cclta_change_title( $translated, $text, $domain )
    {
        remove_filter( current_filter(), __FUNCTION__, 10 );
    
        if ( 'default' === $domain && 'Comment on %s' === $text )
            return 'Talk about %s';
    
        return $translated;
    }
    

    And now the title attribute says: Talk about The Post Title.

  2. Like they said it’s hard coded into WP core, and it’s best practice to not mess with the core. However the title is located in <span class="screen-reader-text">Post Title</span> just use CSS to hide it.

    .screen-reader-text{
    display: none;
    }