Filtering comment permalinks when a condition is met

I’m not completely sure what you are trying to accomplish, but here’s what I think. Normally you would generate a link to a comment like this:

echo '<a href="' . get_permalink($comment->comment_post_ID) . '#comment-' . (strval($comment->comment_ID)) . '">';

The problem with filtering get_permalink() is that the filter must know that it is called by the comments section inside a page, not another part of the same page. There is no obvious way to do this, except if you bypass the built in filter system and call your function on the spot:

$permalink = my_comment_link_filter (get_permalink($comment->comment_post_ID));
echo '<a href="' . $permalink . '#comment-' . (strval($comment->comment_ID)) . '">';

Remember, you must also have some redirect in place, so WP will understand the permalink. Also, this approach means that clicking on the permalink of comment in the page will cause the page to reload, because it’s a different url.

Leave a Comment