14th Nov '22
/
4 comments

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 630 Bricks code tutorials with BricksLabs Pro

4 comments

  • David Nickson

    This is amazing!

    When I submit a comment the page reloads and just takes you to the top of the blog. This means users don't have any signal that their comment sent when you have manual approval on.

    Is there a way to not make it reload and instead show a message saying "Thank you for submitting your comment, a moderator will review it."?

    Cheers

  • Usman Khurshid

    This worked for me. Thank you. But if I want to unlink all the URLs in comments, how would I be able to do that in Bricks?

    Also, how can I add the notify me when new comments are added like yours?

Leave a Reply to Sridhar Katakam (Cancel Reply)

 

Related Tutorials..

Pro
How to programmatically add a class to an element in Bricks

How to programmatically add a class to an element in Bricks

This Pro tutorial shows how a dynamic class can be added to a query loop element in Bricks programmatically. Let's take an example where posts…
Pro
Filtering Meta Box Cloneable Group by Select Subfield for Multiple Bricks Query Loops with Conditional Output

Filtering Meta Box Cloneable Group by Select Subfield for Multiple Bricks Query Loops with Conditional Output

In the Bricks Facebook group a user asks: Consider this cloneable Meta Box field group for a Custom Post Type called Tour: with the Tour…
Pro
“Truncate text to these many characters” Bricks Control

“Truncate text to these many characters” Bricks Control

Bricks provides a :<number> dynamic data tag modifier that can be used to limit the amount of text by the specified number of words. Ex.:…
Pro
Outputting Only the First ACF Repeater Row in Bricks

Outputting Only the First ACF Repeater Row in Bricks

Updated on 12 Dec 2023 In the Bricks Facebook group a user asks: How can I display only the first entry from an ACF repeater?…
How to create filters with IsotopeJS in Bricks (Part 1)

How to create filters with IsotopeJS in Bricks (Part 1)

This tutorial series will explore the IsotopeJS library's features inside the Bricks ecosystem.
Categories:
Pro
How to Inject a Different Type of Post in Bricks Query Loop

How to Inject a Different Type of Post in Bricks Query Loop

Updated on 26 Aug 2024 A user asks: Hello, I'd like to inject a CPT post card inside of a Bricks query loop of a different CPT…
Pro
Filtering a Bricks Terms query on Term archive pages

Filtering a Bricks Terms query on Term archive pages

On product category archives, how to show product type names but only those that the current product category has.
How to Add a Custom Control Field to an Element in Bricks

How to Add a Custom Control Field to an Element in Bricks

In this tutorial, we'll learn how to add a custom control field to an element and dynamically change a CSS property inside the builder. Introduction…
Categories:
Tags: