I can't figure out how to filter the comment permalink on certain WP page that I use like this:

if(condition is met) {
  (filter the comment url)

...so that all the comment permalinks inside the page can be changed from this:


to this:


In short, I'm trying to change the url structure pointing out to the page permalink, excluding the site_url (front page url) and the comment slug (e.g. #comment-n)

So far, I've tried the example in the comment_link filter and nothing happens here:

function my_comment_link_filter( $link ) {
    $link = str_replace( get_permalink() , $new_permalink_structure , $link );
    return $link;
add_filter( 'comment_link', 'my_comment_link_filter', 10, 3 );

I'm doing it wrong, it seems, and would certainly appreciate any help.

  • 1
    This filter only changes the link in the RSS feed. Also I don't see $new_permalink_structure defined anywhere. – cjbj May 28 '16 at 13:27
  • I know. I also know that what I've tried earlier is wrong. That's why I made this question, since I don't know which filter should be used. – 58YtQ2H83m17838963l61BU07Y8622 May 28 '16 at 14:42
  • I think what I'm looking for is related to this filter: developer.wordpress.org/reference/functions/get_comments_link – 58YtQ2H83m17838963l61BU07Y8622 Jun 1 '16 at 9:32
  • Ah yes, I forgot about that one. That solves the problem of only changing permalinks in the comments part of a page. Well done! – cjbj Jun 1 '16 at 11:23

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.

  • Actually, I'm using a plugin that has a custom url rewrite and uses a shortcode in a page to generate newer pages of its own. I managed to integrate WP comments of the page for each newly generated pages by the plugin using comment meta keys/values but these comments still uses the original permalink of the said page so clicking them would get me redirected to the original page instead. That's what I'm trying to avoid. – 58YtQ2H83m17838963l61BU07Y8622 May 28 '16 at 17:38
  • That's pretty complicated. I'm afraid that one needs insight in the plugin to understand what is going on and how to solve the situation. – cjbj May 28 '16 at 18:29
  • To simplify it: I have dynamic pages generated from a wordpress page by a shortcode. Since I'm using the wordpress comments for each dynamic pages, I want these comments to be the same permalinks instead of the original permalink of the page. – 58YtQ2H83m17838963l61BU07Y8622 May 28 '16 at 18:32
  • That means by the time you get the permalink you also know the dynamic page slug and you can pass them both to the filter function – cjbj May 28 '16 at 18:54
  • Yup, the dynamic page slug and url are both available in variable and can be called in the functions.php. – 58YtQ2H83m17838963l61BU07Y8622 May 28 '16 at 20:47

I found an exact solution I'm looking for.

function my_comment_link_filter( $link ) { 
    $link = str_replace( get_permalink() , $custom_permalink , $link );
    return $link; 
add_filter( 'get_comment_link', 'my_comment_link_filter' );

It does exactly what I am looking for: It replaces the Page Permalink with the dynamic permalinks when I call the filter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.