Support » Requests and Feedback » Scroll comment after posting it

  • When someone posts a comment, I think it’d be cool if it went to the comment after editing it.

    The file that needs editing would be wp-comments-post.php of course.

    So, here’s my suggested code.

    Original code:

    $location = (empty($_POST['redirect_to'])) ? $_SERVER["HTTP_REFERER"] : $_POST['redirect_to'];

    Modified code:

    $comment_ID = $wpdb->get_var("SELECT comment_ID FROM $wpdb->comments WHERE comment_post_ID = '$comment_post_ID' AND comment_author = '$comment_author' AND comment_author_email = '$comment_author_email' AND comment_author_url = '$comment_author_url' AND comment_content = '$comment_content' ORDER BY comment_ID DESC");

    $location = (empty($_POST['redirect_to'])) ? $_SERVER["HTTP_REFERER"] . '#comment-' . $comment_ID : $_POST['redirect_to'];

    Can anyone think of any issues or drawbacks of doing this? I wouldn’t think it’d break anything ’cause if comment ID doesn’t exist (i.e. due to it not being in the theme or it being a different page), it still displays the intended page.

    I’m using it on my dev blog and it seems to work fine. 🙂

Viewing 6 replies - 1 through 6 (of 6 total)
  • Okay, I’ve determined a flaw in my code. When WordPress inserts a comment into the database, it seems to apply at least 1 type of formatting to it before it puts it into the database — it adds rel="nofollow" to links.

    This causes no comment ID to be returned.

    If I weren’t so lazy, I’d apply the same to $comment_content so that we’ll always get a result, but instead, we’ll just test to see if we have a result.

    Replace this in your modified wp-comments-post.php:

    $location = (empty($_POST['redirect_to'])) ? $_SERVER["HTTP_REFERER"] . '#comment-' . $comment_ID : $_POST['redirect_to'];

    with this:

    $comment_anchor = (empty($comment_ID)) ? '#comments' : '#comment-' . $comment_ID;

    $location = (empty($_POST['redirect_to'])) ? $_SERVER["HTTP_REFERER"] . $comment_anchor : $_POST['redirect_to'];

    That’ll go to #comments if there’s no comment ID returned.

    However, if this were to be actually implemented into WordPress (which it hopefully will be), it’d just be easier for wp_new_comment() to return the comment’s ID number:

    $comment_ID = wp_new_comment($commentdata);


    Well that’s one way, but I prefer to use this for comment.php:

    <input name="submit" type="submit" id="submit" tabindex="6" value="Submit Comment" />
    <input type="hidden" name="comment_post_ID" value="<?php echo $id; ?>" />
    <input type="hidden" name="redirect_to" value="<?php echo convert_chars($_SERVER['REQUEST_URI']); ?><?php echo "#comment-" ?><?php comment_ID() ?>"/>

    Though it takes you to the last comment before you new one. So off by one. But fine with me because its close enough. 😛

    However, if someone can figure out how to add 1 to it so it goes to the last comment instead of the previous before you submited your new one please let me know.

    Take Care,


    Will, the problem with just adding 1 (or any value) is there’s no guarantee the following comment will actually be assigned the next incremental ID in the database.

    Um you sure thats a problem doing it this way?

    Because when you make the comment it will always be the lastest comment and should be the next incremetal id so plus 1, no? If not then how could I grab it from the database to be sure?

    Each comment added increments the comment ID by one. But that includes comments (and trackbacks, and pings) to all other posts on your blog, as well as spam comments that never make it to the light of day. It’s possible where the first comment to a post has the comment ID of 10 that the next receives 11, but then again it could get 184.


    Oh ok now i understand.



Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Scroll comment after posting it’ is closed to new replies.