comment_form() generates the wrong action url

I created the file comments.php. Inside it I have a manual loop for my comments section. I loop from offset = 0 + ((page - 1) * total_per to offset + total_per). The problem is when I add new comments, the comment form’s action field has the wrong URL generate. It consequently goes to the very last page of comments (ie. the oldest).

I had a question regarding this that I posted earlier. However after tracing some of the WP source code around I was able to find more information, so I deleted the original.

How can I ensure my theme’s comment form generates the correct action URL without editing wp-includes/comment-template.php or wp-comments-post.php?

Line 734 in wp-includes/comment-template.php is causing the problem. I don’t understand why though. I changed my theme to copy cpage, so I am no longer messing with any expected states. What I do know is that I can resolve my problem by hacking up the line in the core file. This change makes the generated URL go to the correct location.

if ( 'newest' === get_option( 'default_comments_page' ) )

Originally this.

if ( 'oldest' === get_option( 'default_comments_page' ) && 1 === $cpage )

It doesn’t make sense to me that it disregards the cpage value for the URL, ONLY if the option is set to ‘oldest’. I would think it should consider when to disregard cpage in either situation. It doesn’t though, why is it hard coded to only consider when it is Oldest at the top has the default page set to oldest, and cpage is at page 1? This makes no sense!

What am I doing to cause get_comment_link() to generate the wrong page value? Is there a way for me to easily debug these things? I tried adding echos to the core code, but they didn’t show up.

Here is all my comment form related source code. (I forgot to add the namespace for my functions “wbs”, use the power of imagination because I can’t edit the pastebin of it.
comments.php(top) and functions.php(bottom) at http://pastebin.com/aer07uvF

Sorry I can only post 2 links with <10 rep.

edit:

So comment_form() generates the correct URL, because the comment-page-1 should always have the oldest comments.

1 Answer
1

By default WordPress organizes the comment pages from oldest to newest. This does not change, even if the Settings-Discussion options have been modified. This is the sticking point, one might suspect changing these settings to reorganize the comment pages, it doesn’t. These settings, basically, define comment order in the default comment loop, and what page is displayed when $cpage is empty.

To be clear, when $cpage is empty that means you’re on the Post URL and not a comment page within that post.

This means, the problem isn’t in getting the links, since that code is not dynamic. The problem is in how comments are displayed. There are two options 'default_comments_page' and 'comment_order' each has two settings, which makes for four configurations. Each needs a unique offset calculation. In this code below you can see the offset calculations necessary to display the correct comments on the correct pages.

function grab_comments( $remainder, $page_total){
    global $cpage;
    $per_page = get_option('comments_per_page');
    $order_asc = get_option('comment_order') == 'asc';
    $order = $order_asc ? 'ASC' : 'DESC'; //Affects the offset

    if( get_option('default_comments_page') == 'newest' )
    {
        if ($cpage == '') $cpage = $page_total;

        if (get_option('comment_order') == 'desc')
        {
            // ; 8,7,6; 5,4,3; 2,1,0
            $offset = ( $page_total - $cpage ) * $per_page;
        }
        else
        {
            // ; 6,7,8; 3,4,5; 0,1,2
            $offset = ( ( $cpage - 1 ) * $per_page ) - ( $per_page - $remainder );
            if($offset < 0){
                $offset = 0;
                $per_page = $remainder != 0 ? $remainder : $per_page;
            }
        }
    }
    else // default page: 'oldest'
    {
        if ($cpage == '') $cpage = 1;

        if (get_option('comment_order') == 'asc')
        {
            // 0,1,2; 3,4,5; 6,7,8;
            $offset = ( $cpage - 1 ) * $per_page;
        }
        else
        {
            // 2,1,0; 5,4,3; 8,7,6;
            $offset = ( ( $page_total - $cpage ) * $per_page ) - ($per_page - $remainder);
            if($offset < 0){
                $offset = 0;
                $per_page = $remainder != 0 ? $remainder : $per_page;
            }
        }
    }
    $args = array(
        'post_id' => get_the_id()
        , 'number' => $per_page
        , 'offset' => $offset
        , 'order' => $order);
    return get_comments($args);
}

Leave a Comment