Make WordPress Core

Opened 3 years ago

Last modified 5 weeks ago

#60281 new defect (bug)

Cannot unset comment_notes_before

Reported by: bugnumber9 Owned by:
Priority: normal Milestone: Future Release
Component: Comments Version: 3.0
Severity: normal Keywords: has-patch has-unit-tests
Cc: Focuses:

Description

I want to reorder comment fields by unsetting and setting them back in my chosen order. Here's my code:

function my_reorder_comments_fields( $fields ) {
	$comment_notes_before_field = $fields['comment_notes_before'];
	$comment_field = $fields['comment'];
	$author_field = $fields['author'];
	$email_field = $fields['email'];
	$url_field = $fields['url'];
	$cookies_field = $fields['cookies'];

	unset( $fields['comment_notes_before'] );
	unset( $fields['comment'] );
	unset( $fields['author'] );
	unset( $fields['email'] );
	unset( $fields['url'] );
	unset( $fields['cookies'] );

	$fields['author'] = $author_field;
	$fields['email'] = $email_field;
	$fields['comment_notes_before'] = $comment_notes_before_field;
	$fields['cookies'] = $cookies_field;
	$fields['comment'] = $comment_field;

	return $fields;
}
add_filter( 'comment_form_fields', 'my_reorder_comments_fields' );

Result - ​https://imgur.com/a/ebY1HBW
You can see that $fields['comment_notes_before'] remains on top.
To prove the point, I changed my code to just unset all fields:

function my_unset_comments_fields( $fields ) {
	unset( $fields['comment_notes_before'] );
	unset( $fields['comment'] );
	unset( $fields['author'] );
	unset( $fields['email'] );
	unset( $fields['url'] );
	unset( $fields['cookies'] );

	return $fields;
}
add_filter( 'comment_form_fields', 'my_unset_comments_fields' );

Result: ​https://imgur.com/a/B5wNngj
So $fields['comment_notes_before'] isn't getting unset while all other fields do.

Change History (7)

#1 @bugnumber9
3 years ago

I also get a notice Undefined index: comment_notes_before that barks at $comment_notes_before_field = $fields['comment_notes_before']; in the first code.

However, it's mentioned in ​https://developer.wordpress.org/reference/functions/comment_form/, and I can access and modify it like this:

function my_change_comments_fields( $fields ) {
	$fields['comment_notes_before'] = '<p>Custom text here.</p>';
	return $fields;
}
add_filter( 'comment_form_defaults', 'my_change_comments_fields' );

This ticket was mentioned in ​Slack in #core by jorbin. ​View the logs.


3 years ago

#3 @jorbin
3 years ago

  • Keywords needs-unit-tests added
  • Version 6.4.2 → 3.0

These filters were introduced in 3.0, so updating the version to that.

I think an automated test that demonstrates what is seen as a bug would be extremely helpful here.

#4 @jorbin
3 years ago

  • Milestone Awaiting Review → Future Release

#5 follow-up: @shooper
3 years ago

Tested this on TwentyTwentyFour with WP 6.4.2.

I think the problem here was the wrong filter being used. The comment_notes_before is not a comment form field, but part of the comment form defaults.

Using the comment_form_defaults filter will unset that value:

function my_update_comment_defaults( $defaults ) {
    $defaults['comment_notes_before'] = '';
    return $defaults;
}
add_filter( 'comment_form_defaults', 'my_update_comment_defaults' );

#6 in reply to: ↑ 5 @bugnumber9
3 years ago

@shooper Looks like it's possible to unset all form fields (I just tested with url field) from comment_form_defaults too. Obviously, the same can be done from comment_form_fields for all fields except comment_notes_before.
Besides that, comment_notes_before is listed in ​https://developer.wordpress.org/reference/functions/comment_form/ and HTML-wise it's a part of the form - ​https://imgur.com/a/3tH6JBN

This ticket was mentioned in ​PR #13460 on ​WordPress/wordpress-develop by ​@akshayjain.


5 weeks ago
#7

  • Keywords has-patch has-unit-tests added; needs-unit-tests removed

## Trac ticket
https://core.trac.wordpress.org/ticket/60281

## Summary
Adds unit test coverage for how comment_notes_before behaves in comment_form().
It is part of the comment form *defaults* and is rendered outside the fields loop,
so it is not an entry in the array passed to the comment_form_fields filter —
unset()-ing it there has no effect. The supported way to remove it is to empty it
via the comment_form_defaults filter. Addresses @jorbin's request for an automated
test demonstrating the reported behavior.

## Changes

  • tests/phpunit/tests/comment/commentForm.php: three new tests. No production code changes.

## Testing

  • phpunit tests/phpunit/tests/comment/commentForm.php → 17 pass
  • phpunit --group comment → 642 pass, no regressions
  • phpcs --standard=phpcs.xml.dist clean

## Backward compatibility
None. Test-only addition.

## AI disclosure
Prepared with AI assistance (Claude Code / Claude Opus). A human contributor reviewed
the change, ran the full suite locally, and verified correctness before submission.

Note: See TracTickets for help on using tickets.