Modifications of the core code are not recommended.
Note that the core get_comments_link()
function considers two cases of $hash
:
$hash = get_comments_number( $post_id ) ? '#comments' : '#respond';
Also note the second input argument is $post_id
.
Here’s your modified example:
add_filter( 'get_comments_link', function( $link, $post_id )
{
$hash = get_comments_number( $post_id ) ? '#mycomments' : '#myrespond';
return get_permalink( $post_id ) . $hash;
}, 10, 2 );
or another approach might be a simple replacement:
add_filter( 'get_comments_link', function( $link, $post_id )
{
return str_replace(
['#comments', '#respond'],
['#mycomments','#myrespond'],
$link
);
}, 10, 2 );
But it sounds like you want to modify the get_comment_link()
according to your line number reference. Then you should consider the get_comment_link
filter:
add_filter( 'get_comment_link', function( $link, \WP_Comment $comment )
{
return str_replace(
'#comment-',
'#mycomment-',
$link
);
}, 10, 2 );