Divi WordPress Them — the smartest and most flexible WordPress theme

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:

  1. $location = empty($_POST['redirect_to']) ? get_comment_link($comment_id) : $_POST['redirect_to'] . '#comment-' . $comment_id;
$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 http://example.com/this-is-a-post/comment-page-3/#comment-201 (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:

  1. $location = empty($_POST['redirect_to']) ? get_comment_link($comment_id, array('type' => 'comment')) : $_POST['redirect_to'] . '#comment-' . $comment_id;
$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!

References

  1. http://codex.wordpress.org/Function_Reference/get_comme ... mment_link []
Print Article Trackback Trackback to this Article   Subscribe to Comments RSS Subscribe to Comments RSS

3 Opinions for Correct Redirection after Posting a Comment

  1. User's Gravatar
    1
    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
    2
    منتديات March 30, 2012 at 8:45 am – Permalink

    Thaaanks for youu …Nice

  3. User's Gravatar
    3
    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.
    http://www.oyetweet.com/country/usa/?celebid=Tony%20Hawk

    URI After comment.
    http://www.oyetweet.com/country/usa/?celebid=Tony%20Hawk

    So no change in URI.

    Possible ?

    Cheers,
    -Tony

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: