making bbPress (and WordPress) work better!

paged comments enhancement – jump to calculated page for any requested comment

While I love the paged comment plugin (last updated April 2006) and consider it excellent coding, there is (at least) one or two problems with the way WordPress operates when comments are paged.

If comments are paged, WordPress should act more like a forum, ie. when someone posts a comment they should be returned to the last page where their new post appears, so they can see any posts afterwards, etc.

Additionally, if search enhancedments are used to include comments in searches, the resulting links should be able to take the searcher to the exact comment.

On paged comments, both these tasks are virtually impossible. Some people solve it by forcing paged comments to put the newest comments ON TOP of older comments. However this breaks the “forum style” of comment flow. Now you don’t have to resort to that behavior.

I’ve decided to tackle the first part of post-redirect to the new comment, first.
With flat “view all” comments because you just need to know the new comment id and you can hack wp-comments-post.php and just do something like this (at the very end):

$location = ( empty( $_POST['redirect_to'] ) ) ? get_permalink($comment_post_ID)."#comment-".$comment_id : $_POST['redirect_to'];
wp_redirect( $location );

Note the part in bold which is the only addition. Doing this as a hack is necessary because I don’t know how to hook that functionality otherwise (if you know how, please speak up). Inserting it as a hidden $_post “redirect_to” is not possible since the new comment_id does not exist yet and there may be additional comment posts happening as the visitor is viewing the very comments .(Why WP doesn’t have this simple addition as the default and just returns the visitor to the top of the post is a mystery to me, among many WP mysteries😉 )

But making this work with paged-comments is extrodinarily tricky. I’ve tackled (for now) comments that are done in forum style, date assending order (oldest to newest) and do NOT fill the last page.

The code to add in wp-comments-post.php is listed below the break. It is tested but not for all situations or extensively. Any feedback/improvements are appreciated.

replace the last two lines of wp-comments-post.php with the following code to have it return the comment poster to the actual page and position of their post.

Basically this adds a function (which actually belongs inside the paged-comments.php but I’m being lazy for now) called get_pagedlink which requires the comment_id you are looking for and optionally if you already have it, the post_id that the comment belongs to (this just saves one database call if you are coming from a function that already knows it) otherwise it will figure out the original post_id itself. It returns the comment-page-x/ where x is the page the comment belongs on for that visitor (calculating any hidden comments that are pending approval). If the paged-comments are not running or the comments only have one page it returns nothing.

Note this is unfinished but working code (works only if you have comments in oldest to newest order – for now).

$location = ( empty( $_POST['redirect_to'] ) ) ? get_permalink($comment_post_ID).get_pagedlink($comment_id,$comment_post_ID)."#comment-".$comment_id : $_POST['redirect_to'];

wp_redirect( $location );

function get_pagedlink ($comment_id, $comment_post_ID=0) {
global $wpdb, $paged_comments;
if (function_exists('paged_comments_print_pages')) {
if (($paged_comments->all_posts == true) && ($paged_comments->all_pages == true)) {
// $paged_comments->fill_last_page = false; - to do: calculate filled end page
// $paged_comments->default_page = 'auto'; - to do:
// $paged_comments->ordering = 'DESC'; - to do:
if ($comment_post_ID==0) {$comment_post_ID=(int)$wpdb->get_var("SELECT comment_post_ID FROM $wpdb->comments WHERE comment_id = '$comment_id' LIMIT 1");}

$comment_author = isset($_COOKIE['comment_author_'.COOKIEHASH]) ? trim(stripslashes($_COOKIE['comment_author_'.COOKIEHASH])) : '';
$comment_author_email = isset($_COOKIE['comment_author_email_'.COOKIEHASH]) ? trim(stripslashes($_COOKIE['comment_author_email_'.COOKIEHASH])) : '';
$comment_author_url = isset($_COOKIE['comment_author_url_'.COOKIEHASH]) ? trim(stripslashes($_COOKIE['comment_author_url_'.COOKIEHASH])) : '';
if (empty($comment_author)) {
$approved_condition = "comment_approved = '1'";
} else {
$author_db = addslashes($comment_author);
$email_db = addslashes($comment_author_email);
$approved_condition = "(comment_approved = '1' OR (comment_author = '$author_db' AND comment_author_email = '$email_db' AND comment_approved = '0'))";
// SQL for paged comments
$post_comment_ids = $wpdb->get_col("SELECT comment_id FROM $wpdb->comments WHERE comment_post_ID = '$comment_post_ID' AND $approved_condition ORDER BY comment_date ".($paged_comments->ordering));
$pagedlink = ceil((intval(array_search($comment_id, $post_comment_ids))+1) / ($paged_comments->per_page));
// echo $comment_id." =a= ".$comment_post_ID." =b= ".$pagedlink." =c= ".intval(array_search($comment_id, $post_comment_ids))." =d= ".($paged_comments->per_page);
// print_r($post_comment_ids);
if (intval($pagedlink)>0) {
if ($paged_comments->fancy_url == true) {$pagedlink='comment-page-'.$pagedlink."/";}
else {$pagedlink='?cp='.$pagedlink;}
} else {$pagedlink='';}
return $pagedlink;

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s