Skip to content

feat(comments): add options to collapse comments section#2717

Open
faisalahammad wants to merge 1 commit into
Automattic:trunkfrom
faisalahammad:fix/1430-collapse-comments-options
Open

feat(comments): add options to collapse comments section#2717
faisalahammad wants to merge 1 commit into
Automattic:trunkfrom
faisalahammad:fix/1430-collapse-comments-options

Conversation

@faisalahammad
Copy link
Copy Markdown
Contributor

@faisalahammad faisalahammad commented May 15, 2026

Summary

Replace the boolean "Collapse Comments" checkbox with a select control offering three options: don't collapse, collapse when more than one comment, and always collapse. This allows publishers to always collapse the comments section regardless of comment count.

Fixes #1430

Changes

newspack-theme/inc/customizer.php

Before:

$wp_customize->add_setting(
    'collapse_comments',
    array(
        'default'           => false,
        'sanitize_callback' => 'newspack_sanitize_checkbox',
    )
);
$wp_customize->add_control(
    'collapse_comments',
    array(
        'type'        => 'checkbox',
        'label'       => esc_html__( 'Collapse Comments', 'newspack-theme' ),
        'description' => esc_html__( 'When using WordPress\'s default comments, checking this option will collapse the comments section when there is more than one comment, and display a button to expand.', 'newspack-theme' ),
        'section'     => 'comments_options',
    )
);

After:

$wp_customize->add_setting(
    'collapse_comments',
    array(
        'default'           => '',
        'sanitize_callback' => 'newspack_sanitize_select',
    )
);
$wp_customize->add_control(
    'collapse_comments',
    array(
        'type'        => 'select',
        'label'       => esc_html__( 'Collapse Comments', 'newspack-theme' ),
        'description' => esc_html__( 'When using WordPress\'s default comments, choose when to collapse the comments section and display a button to expand.', 'newspack-theme' ),
        'section'     => 'comments_options',
        'choices'     => array(
            ''              => esc_html__( 'Don\'t collapse', 'newspack-theme' ),
            'more_than_one' => esc_html__( 'Collapse when more than one comment', 'newspack-theme' ),
            'always'        => esc_html__( 'Always collapse', 'newspack-theme' ),
        ),
    )
);

Why: Provides granular control over when comments collapse. Added newspack_sanitize_select() for proper select validation.

newspack-theme/comments.php

Before:

if ( $collapse_comments && 1 < (int) $discussion->responses && $on_first_page ) {
    $comments_collapsed = true;
}

After:

if ( $collapse_comments && $on_first_page ) {
    if ( 'always' === $collapse_comments ) {
        $comments_collapsed = true;
    } elseif ( ( true === $collapse_comments || 'more_than_one' === $collapse_comments ) && 1 < (int) $discussion->responses ) {
        // Backward compat: boolean true treated same as 'more_than_one'.
        $comments_collapsed = true;
    }
}

Why: Supports the three collapse modes. Boolean true from existing installations maps to "collapse when more than one comment" for backward compatibility.

Also added collapse wrapper around the comment form in the else branch (when no comments exist) so "Always collapse" works with 0 comments.

Testing

Test 1: Don't collapse (default)

  1. Appearance → Customize → Comments Settings → "Don't collapse"
  2. Visit post with comments — displays normally, no collapse button

Test 2: Collapse when more than one comment

  1. Set to "Collapse when more than one comment"
  2. Post with 0–1 comment: displays normally
  3. Post with 2+ comments: collapsed with "Expand comments" button

Test 3: Always collapse

  1. Set to "Always collapse"
  2. Post with 0 comments: comment form collapsed with button
  3. Post with 1+ comments: collapsed with button

Test 4: Backward compatibility
Existing installations with checkbox enabled continue to work as "collapse when more than one comment".

Build

newspack-theme-fix-1430.zip available for manual testing. Install via WP Admin → Appearance → Themes → Upload Theme.

Screenshot

image . image . image

Replace the boolean "Collapse Comments" checkbox with a select control
offering three options: don't collapse, collapse when more than one
comment, and always collapse. This allows publishers to always collapse
the comments section regardless of comment count.

Backward compatible: existing boolean true values map to "collapse when
more than one comment" behavior.

Fixes Automattic#1430
@faisalahammad faisalahammad requested a review from a team as a code owner May 15, 2026 20:19
@laurelfulford laurelfulford added the [Status] Needs Review The issue or pull request needs to be reviewed label May 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Status] Needs Review The issue or pull request needs to be reviewed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Collapse comments even if one or none

2 participants