Make WordPress Core

Opened 6 weeks ago

Last modified 6 weeks ago

#60281 new defect (bug)

Cannot unset comment_notes_before

Reported by: bugnumber9's profile bugnumber9 Owned by:
Milestone: Future Release Priority: normal
Severity: normal Version: 3.0
Component: Comments Keywords: needs-unit-tests
Focuses: Cc:

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 (6)

#1 @bugnumber9
6 weeks 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.


6 weeks ago

#3 @jorbin
6 weeks ago

  • Keywords needs-unit-tests added
  • Version changed from 6.4.2 to 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
6 weeks ago

  • Milestone changed from Awaiting Review to Future Release

#5 follow-up: @shooper
6 weeks 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
6 weeks 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

Note: See TracTickets for help on using tickets.