Back to Top

Correct Redirection after Posting a Comment

Previous Post:

Correct Redirection after Posting a Comment

Separating comments and trackbacks is one of the first things many WordPress users would do when they have a fresh WordPress installation. The benefit is clear, but is there any catch? Just post a new comment when separation is active and paged comments is enabled, you will be magically redirected to a page with no comments rather than pingbacks/trackbacks.

Now I have good news and bad news for you. The good news is, ‘magically’ can actually mean ‘logically’ sometimes, and the bad news is, to fix this you will have to, as far as I know, hack a core file. That core file, as you might have guessed, can be no other than wp-comments-post.php.

So, let’s have a good look at the guilty file, shall we? Scroll down to line 100 and you should see this:

$location = empty($_POST['redirect_to']) ? get_comment_link($comment_id) : $_POST['redirect_to'] . '#comment-' . $comment_id;

What’s causing the issue is merely this function: get_comment_link($comment_id)1.

It uses an interesting argument, ‘type’, and according to the codex page, that argument allows you to specify the type of comment used in generating the redirection URL, i.e. the URL to the comment page a visitor will land on after posting a comment.

Basically, when you use get_comment_link($comment_id) without the ‘type’ argument you are telling WordPress to redirect visitors to a page that is expected to have comments and pingbacks all in one place. When you separate pingbacks from real comments and list all of them under your blog post, you are obviously need less comment pages than needed.

For example you have a popular post with 200 comments in total of which 20 are pingbacks, and you choose to show just 100 comments per page. When someone posts the 201st comment, WordPress will normally redirect him/her to (assuming that all comments are top-level).

The problem is you will have only 181 real comments (180 plus the newly submitted one) which is less than 200, which means no real comment should be shown for comment-page-3, right?

To tell the truth, fixing the issue is much easier than explaining it, so I think you might have figured out the workaround already. Just change the 100th line of wp-comments-post.php to this:

$location = empty($_POST['redirect_to']) ? get_comment_link($comment_id, array('type' => 'comment')) : $_POST['redirect_to'] . '#comment-' . $comment_id;

and… Voila, the redirection is now correct!

Remember to make this change to the guilty file whenever WordPress is updated, though. It’s not a fun task, I know, but until there’s a hook for that function, this is the only solution I can come up with. If someone has a better idea, please comment below, thanks!


  1. ... mment_link []

Take Social Sharing to
the Next Level with Monarch!

Take Social Sharing to the Next Level with Monarch!
Print Article Trackback Trackback to this Article   Subscribe to Comments RSS Subscribe to Comments RSS

5 Opinions for Correct Redirection after Posting a Comment

  1. User's Gravatar
    MNDS October 19, 2011 at 8:43 pm – Permalink

    Hacking the core file is something that I never knew . Sad but true . And reading this your explanation gives me only one thought that you already wrote : “it seems that fixing issue is easier then to explain it or understand ” .

  2. User's Gravatar
    منتديات March 30, 2012 at 8:45 am – Permalink

    Thaaanks for youu …Nice

  3. User's Gravatar
    tony December 10, 2012 at 6:34 pm – Permalink

    How to make this work on following condition.

    1. I want comment to post and appear in in page but my page url should not be changed even with query parameters remains same.

    for example,
    URI Before comment.

    URI After comment.

    So no change in URI.

    Possible ?


  4. User's Gravatar
    remsmono January 9, 2015 at 11:11 am – Permalink

    Now You Can Attract Over 17,000 Visitors To Your Sites From Scratch, Totally Automated
    Good luck

  5. User's Gravatar
    Zoodaypayorn February 11, 2015 at 10:46 am – Permalink

    Hi guys
    Cool software Does anyone tried it?
    Have an interesting idea

Speak Up Your Mind!

An asterisk (*) indicates a required field and must be filled.

  • Web page and e-mail addresses turn into links automatically.
  • Wrap codes in: <code lang=""></code> or <pre lang="" extra="">
  • Lines and paragraphs break automatically.

Next Post: