WordPress.org

Ready to get started?Download WordPress

Forums

Scroll comment after posting it (7 posts)

  1. 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. :)

  2. 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);

  3. war59312
    Member
    Posted 8 years ago #

    Hey,

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

    <p>
    <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() ?>"/>
    </p>

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

    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

  4. Kafkaesqui

    Posted 8 years ago #

    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.

  5. war59312
    Member
    Posted 8 years ago #

    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?

  6. Kafkaesqui

    Posted 8 years ago #

    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.

  7. war59312
    Member
    Posted 8 years ago #

    Hey,

    Oh ok now i understand.

    Thanks,

    Will

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags