Skip to content

Filter comments_array not called in render_block_core_comment_template() #47655

@cmautner

Description

@cmautner

Description

This is the code for render_block_core_comment_template() in block_library/comment_template/index.php:

	// Get an array of comments for the current post.
	$comments = $comment_query->get_comments();
	if ( count( $comments ) === 0 ) {
		return '';
	}

	$comment_order = get_option( 'comment_order' );

	if ( 'desc' === $comment_order ) {
		$comments = array_reverse( $comments );
	}

	$wrapper_attributes = get_block_wrapper_attributes();

	return sprintf(
		'<ol %1$s>%2$s</ol>',
		$wrapper_attributes,
		block_core_comment_template_render_comments( $comments, $block )
	);

That doesn't allow a plugin author to provide a custom ordering. In my case I am adding comment voting and we want to reorder comments by their up/down votes. The following line should be inserted prior to the $wrapper_attributes assignment:

	$comments = apply_filters( 'comments_array', $comments, $block->context['postId'] );

Step-by-step reproduction instructions

  1. In any blocks-based theme create a plugin with a comments ordering filter using add_filter( 'comments_array', function ( $comments, $post_id ) { return array_reverse( $comments ); }, 10, 2 );
  2. Create a post and add two or more comments
  3. Load the post. Expected result: is comments in reverse order; Actual result: comments in original order

Screenshots, screen recording, code snippet

No response

Environment info

Wordpress version: 6.1.1, Gutenberg version: Unknown but it matches git, Theme: Twenty TwentyThree
Browser: Chrome, but not pertinent
Device: Host is linux, viewed on linux, Windows, and Android

Please confirm that you have searched existing issues in the repo.

Yes

Please confirm that you have tested with all plugins deactivated except Gutenberg.

Yes

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions