3

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.

5
  • If you want to properly debug core, you should log output to your error log file, typically for WordPress that will be the debug.log file that is generated when WP_DEBUG & WP_DEBUG_LOG constants are defined as true usually within your wp-config.php file. Instead though, create a directory in /wp-content called mu-plugins, so... /wp-content/mu-plugins then copy this gist. Then where you want to debug, call function logger('somemessage', $arg1, $arg2, $etc); - a debug.log file will be generated in wp-content directory.
    – Adam
    Feb 19, 2016 at 4:24
  • Also see Debugging in WordPress for more information about how to debug whilst developing. That's a good starting point. Also, provide your full code in a pastebin.com snippet and link to it in your question above.
    – Adam
    Feb 19, 2016 at 4:27
  • 1
    @userabuser I will enable those, and try your code out when the sun comes back around. I also added a code dump as you asked. I like to think I keep the whitespace maintained so it should be an easy read.
    – Josh C
    Feb 19, 2016 at 5:25
  • What is the URL generated for your comment form action attribute?
    – Adam
    Feb 19, 2016 at 5:55
  • @userabuser The URL is 2016/02/post-2/comment-page-9/#comment-68 I haven't had the chance to throw your debug code in yet, so that is as far as I know.
    – Josh C
    Feb 19, 2016 at 14:41

1 Answer 1

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);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.