3

In Settings > Discussion, there's an option to "break comments into pages" which adds pagination to comments and breaks them up into separate pages.

I want to turn this off across my entire website... BUT, on one specific post, I want this turned on so that post (and that post only) has paginated comments.

Can this be done?

Semi-related follow up: Could this same type of thing be done with the "automatically close comments on posts older than" option? In this case, so that comments on all posts are closed after X days except post 149, 150 and 151?

Thanks in advance for any help.

1 Answer 1

5

The option_{$option_name} filter can be used to modify the value of an option on the fly. Replace {$option_name} with the name of the option to be modified.

Override Comment Pagination Option

The option name for paginating comments is page_comments, so we'll create a filter called option_page_comments. In the example below, we check if we're looking at one of the posts in our array of IDs to force pagination on, and if so, we enforce pagination of comments. Otherwise, the value from the Dashboard > Settings > Discussion screen is used.

// Forces comment pagination for certain posts regardless
// of settings within the Settings > Discussion page.
add_filter( 'option_page_comments', 'wpse_modify_page_comments' );
function wpse_modify_page_comments( $page_comments ) {
    if ( is_admin() ) {
        return $page_comments;
    }

    // Array of post IDs where comment pagination is forced on.
    $force_comment_pagination = [ 
        149,
        150,
        151,
    ];

    if ( in_array( get_the_ID(), $force_comment_pagination ) ) {
        $page_comments = true;
    }

    return $page_comments;
}

Override Close Comments for Old Posts Option

To answer your followup question -- Yes, we can force comments to be enabled for certain older posts even if the settings on the Discussion screen are configured to close comments on old posts.

// Forces comments for old posts to be *allowed* regardless
// of settings within the Settings > Discussion page.
add_filter( 'option_close_comments_for_old_posts', 'wpse_modify_close_comments_for_old_posts' );
function wpse_modify_close_comments_for_old_posts( $close_comments_for_old_posts ) {
    // Don't do anything for the admin area. Return the originally set value of the option.
    if ( is_admin() ) {
        return $close_comments_for_old_posts;
    }

    // This array contains the posts IDs where we want to 
    // override the settings for closing comments for old posts.
    // (Comments will be forced open for these posts.)
    $close_comments_for_old_posts_overrides = [ 
        149,
        150,
        151,
    ];

    // Handle case when a comment is being made.
    if ( isset( $_POST['comment'] ) && isset( $_POST['comment_post_ID'] ) ) {
        if ( in_array( $_POST['comment_post_ID'], $close_comments_for_old_posts_overrides ) ) {
            // Comments should be open for this post.
            return false;
        }               
    }

    // Handle case when post is displayed.
    global $wp_query;
    if ( ! is_array( $wp_query->posts ) ) {
        // There are no posts to display. Don't change the option.
        return $close_comments_for_old_posts;
    }
    foreach ( $wp_query->posts as $post ) {
        if ( in_array( $post->ID, $close_comments_for_old_posts_overrides ) ) {
            // Comments should be open for this post.
            return false;
        }
    }

    // If we get here, return the original value of the option without altering it.
    return $close_comments_for_old_posts;
}
8
  • 1
    Holy crap, that worked! You probably just saved me a month or trying/failing to get that to happen. Thank you!
    – ioutshine
    Apr 21, 2017 at 14:24
  • Follow up question, if you have another minute. Let's say I wanted to make posts 149, 150 and 151 have paginated comments. I assume it's just a matter of adding those post IDs in, but I don't know the correct structure of how that would written? Semi-related follow up: could this same type of thing be done with the "automatically close comments on posts older than" option? In this case, so that comments on all posts are closed after X days except post 149, 150 and 151? Also, is there any way I can contribute a donation to you for your time/help?
    – ioutshine
    Apr 21, 2017 at 14:27
  • Happy to hear that worked! I've edited the original answer so that it works with multiple IDs. I also changed the filter to option_{$option_name} from pre_option_{$option_name} because we really don't need to change the value before it is retrieved. I've also added a solution for allowing comments to be enabled for old posts regardless of the settings in the Discussion panel. Thank you for the offer, but a donation isn't necessary. All I ask is that you click the checkmark next to the answer if you feel like it solved your issue. Have a good one! Apr 21, 2017 at 17:44
  • 1
    Boom! That all worked perfectly. Thank you 100 times over.
    – ioutshine
    Apr 21, 2017 at 18:51
  • 1
    Indeed it does! Thank you yet again for saving the day. Definitely appreciate it.
    – ioutshine
    May 12, 2017 at 16:37

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.