• metalhead

    (@aaronthomas1979)


    Hello,

    I’ve recently began to use pagination in my posts. I’m doing this by adding ‘<!–nextpage–>’ between chunks of content. This splits up long blog posts nicely.

    However, it creates a small quirk. WordPress is giving the reader the option to comment before reaching the end of the blog post.

    For example, I’ve split a blog post into 4 paginated pages. As soon as the reader has read the first page, they already can comment. It seems like they shouldn’t be able to comment until they’ve reached the bottom of the last page (page 4.)

    If this has already been discussed, I apologize, but I’m unable to find anyone asking about this is previous posts.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    I get what you’re saying, but you can’t force people to read the entire post before commenting. I’d swear some people comment just on the title alone!

    Regardless, what you are after is not unreasonable. The comment form being available is just a side effect of how the template works, it’s setup assuming there are no page breaks. It’s possible for the template to conditionally output the comment form only on the final page. You can see how this might be accomplished by looking at the source code for the function that outputs the links to the post’s pages: wp_link_pages()

    It uses several global values to decide how to display the links: $page, $numpages, $multipage, $more
    I’m not sure where these are established, probably the_post(), so as long as the comment form function is called within the loop, the values should still be valid. I’m also not 100% sure how they are used exactly, but I suspect if $multipage is true and $page == $numpages, then the comment form can be output because we are on the final page. Of course, if $multipage is false, the comment form should also be output because there is only one page. Give them a try ??

    Thread Starter metalhead

    (@aaronthomas1979)

    Thanks for your reply. I’m quite the rookie with PHP, so please bear with me.

    wp_link_pages() is located in wp-includes/post-template.php, so I think that means that I’m unable to copy post-template.php over to my child-theme, therefore, I’m reluctant to make any changes to the file.

    What would be a safe way to modify or over-ride that function, without potentially causing problems? Is writing a plugin my best solution? (I’ve never done that before, but it’s never too late to learn, I suppose!)

    Moderator bcworkz

    (@bcworkz)

    Starting your own plugin is just as simple as creating a child theme. Developing what the plugin actually does might be a different story ?? In any case there’s not really a need for a plugin. All necessary code can be placed in your child theme. The only reason for a custom plugin would be for your own organizational reasons, WP doesn’t care which vehicle you use.

    I referenced wp_link_pages() so you can see how the global values are used to decide if the current page is the last (or only). If you’re new to PHP, this added insight is not going to be forthcoming. So be it, you don’t need the function to accomplish your goal. All you need to do is declare any needed globals within the scope of your code. I believe you’ll need all but $more.

    You will need to identify where in your theme the comments and form is output and wrap that code in a conditional if() clause. Just before this, declare global $page, $numpages, $multipage;

    What you need to figure out now is what to put within the if() conditions. I haven’t confirmed this, but I think you only want to output comments and form if either $multipage is not true or if $page == $numpages. FYI, the PHP NOT operator is ! and the OR operator is ||. You should know the equivalence operator is ==.

    See if you can put this all together into something that works.

    Thread Starter metalhead

    (@aaronthomas1979)

    Thanks for your guidance. I was pushing my brain to the limit for a while there, but then I stumbled upon an article that made things easier. (Google destroys brain cells!)

    But in case anyone else wants to accomplish putting comments on the last paginated page of your articles, here’s what I found:

    1) Disable commenting

    2) Add this function to your child theme’s functions.php:

    /**
     * Display the comment form via shortcode on singular pages
     * Remove the default comment form.
     * Hide the unwanted "Comments are closed" message with CSS.
     *
     * @see https://wordpress.stackexchange.com/a/177289/26350
     */
    
    add_shortcode( 'wpse_comment_form', function( $atts = array(), $content = '' )
    {
        if( is_singular() && post_type_supports( get_post_type(), 'comments' ) )
        {
            ob_start();
            comment_form();
            print(  '<style>.no-comments { display: none; }</style>' );
            add_filter( 'comments_open', '__return_false' );
            return ob_get_clean();
        }
        return '';
    }, 10, 2 );

    3) Use this shortcode on the last paginated page of your post: [wpse_comment_form]

    Voila! People can then only comment if they are on the last paginated page of your post. I love it. I hope this works forever. But if not, then refer back to the advice bcworkz was helping me with. It’s always better to do things from scratch, and he gave us the foundation details we need to go that route.

    (Case Closed, for now!)

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Comments on Last Page of Paginated Post’ is closed to new replies.