How to Remove Name and Website Fields in Bricks Comment Forms

Updated on 14 Mar 2024

In the BricksLabs Facebook group a user asked:

What is the best method to remove fields from the Bricks “Comments” element?

I want to remove the “Name” and “Website” fields from the bricks comment element. I’ve tried using snippets that work on the default WP comment form but unfortunately they don’t seem to do anything to the Bricks comments element fields.

I would love some guidance, please! ????

This tutorial provides the steps to remove Name and Website fields from the post comment forms in Bricks.

Before:

After:

Bricks 1.9.7 and Later

Add a Comments element in your single template.

Leave its Source as ‘Bricks’.

From the FIELDS select control, choose Email.

Prior to Bricks 1.9.7

This will also work in 1.9.7 and later.

Step 1

Let’s use comment_form_fields filter to remove the Name and Website fields from the comment forms.

Add this in your child theme’s functions.php or a code snippets plugin:

// Remove Name and Website fields from the comment form
add_filter( 'comment_form_fields', function( $fields ) {
	unset( $fields['author'] ); // Name field
	unset( $fields['url'] ); // Website field

	return $fields;
} );

Step 2

While we have removed the mandatory Name field from the frontend, the form still treats it as a mandatory field and won’t let us submit the form unless it is filled.

In the WP admin, go to Settings → Discussion and uncheck “Comment author must fill out name and email” under “Other comment settings”.

Step 3

Since WordPress only allows us to remove the required condition for both Name and Email as a pair, we now need a way to make the Email field required.

Add this in your child theme’s functions.php or the code snippets plugin:

// Make Email field in comment forms required.
add_filter( 'preprocess_comment', function( $fields ) {
	if ( $fields['comment_author_email'] == '' ) {
		wp_die( 'Error: Please enter a valid email.' );
	}

	return $fields;
} );

Step 4

Let’s add an asterisk next to the “Email” label in the form to indicate to the visitor that it is a required field.

Edit your single post template and add this at Settings (gear icon) → PAGE SETTINGS → CUSTOM CODE → Custom CSS:

.comment-form .form-group:first-child label::after {
  content: " *";
}

References

https://developer.wordpress.org/reference/hooks/comment_form_fields/

https://developer.wordpress.org/reference/hooks/comment_form_default_fields/

https://forum.bricksbuilder.io/t/standard-wp-filters-for-comment-form-dont-work-with-comment-form-element/6637?u=sridhar

https://www.howtosolutions.net/2015/10/wordpress-comments-making-email-field-optional/

https://wordpress.stackexchange.com/a/294397/14380

add_filter( 'comment_form_fields', 'bricks_comment_form_fields_order' );

in wp-content/themes/bricks/includes/utilities/comments.php.

Get access to all 561 Bricks code tutorials with BricksLabs Pro

4 comments

Leave your comment

 

Related Tutorials..

Pro
Prefiltering Bricks Terms Query Loop

Prefiltering Bricks Terms Query Loop

Let's say there's a Events CPT with a 'Event Year' custom taxonomy. Sample event year names could be: 2021, 2022, 2024, 2025, 2028 etc. We…
Pro
Conditionally Hiding Bricks Filters based on Select Filter Value

Conditionally Hiding Bricks Filters based on Select Filter Value

How show or hide Bricks' filter based on the selection made by another filter.
Categories:
Tags:
Pro
Restrict Content Pro in Bricks

Restrict Content Pro in Bricks

This Pro tutorial provides the steps to restrict Pages and individual elements like Sections in Bricks depending on whether the current user has any active…