WordPress : How to make email field not required in comments

wordpress-comment-form-making-while-leaving-name-required

Recently I was thinking of ways to get more comments on my WordPress blog and one way to achieve that could be making the email field optional instead of required as some visitors might be unwilling to share their email just to drop a comment. It turns out this is not as easy as it sounds. It seems the WordPress has Name and Email tied up as you can only make either both optional or both required. This post will show you, how to make email an optional field while leaving the name field required.

To achieve this, we need to use a WordPress filter and modify some code in the comments.php file of the template, but before all that, we first need to make sure that a specific discussion related option in the Dashboard is unchecked.

Note:If you are using "A comment is held for moderation" option under Discussion settings, be aware that when making email field optional, the users who comment on your posts without providing their email address will not see "Your comment is awaiting moderation." message!. Later in the article we will modify our code to get around this problem.

Step 1 – Change the option in WordPress Dashboard for comments

Under Dashboard > Settings > Discussion, remove the checkbox for “Comment author must fill out name and e-mail” as shown below:

Wordpress discussion option for comments about author filling out name and email

This will make both the name and e-mail field optional.

Next, we need to make sure that even though WordPress will consider the name field as not required, the comment will not be submitted if the name field is empty.

We achieve that using preprocess_comment filter, which is shown next.

Step 2 – Use 'preprocess_comment' filter to make Name field required

Add the following code in functions.php:

function require_comment_name($fields) {

if ($fields['comment_author'] == '')
wp_die('Error: please enter a valid name.');

return $fields;
}
add_filter('preprocess_comment', 'require_comment_name');

The above code will make sure that the comment will not be posted with an empty name field.

Since the WordPress is still showing both Name and E-mail as not required input fields, we need to do some customization of the comment form, so that it shows Name field as required and at the same time point out to the visitors, that the email field is only optional.

We do this by modifying the comment.php file, which is shown next.

Step 3 – Edit comment.php file in the template

Open the comment.php file. Inside it, there should be code for displaying already existing comments and to show comment form for adding a new comment.

Some templates might simply call WordPress function comment_form() to show default comment form, while other templates might contain HTML code for each of the fields. Depending on which template you have, do one of the following:

  • For Templates containing HTML code of comment fields in comment.php

    First, let’s make sure the visitor knows, the email is not a required field.

    Modifying code for the Email field

    Find the code for email field in the comment form. It should look something like this:

    <label for="email">Email ( <?php if ( $req ) echo "required, "; ?>never shared )</label>

    We change that to

    <label for="email">Email ( "optional, never shared )</label>
    

    Here, we removed the check for $req value and simply hardcoded optional message.

    Modifying code for the Name field

    The name will act as being required thanks to code from step 2 but WordPress still considers it as an optional field and will display it as such.

    We will do the same what we did with e-mail above, removing $req check and hardcoding the required message.

    Find the code that looks something like this:

    <label for="author">Name <?php if ( $req ) echo "( required )"; ?></label>

    and change it to:

    <label for="author">* Name ( required )</label>
    

    Modifying code for the aria-required Attribute for Name field

    This attribute is used to indicate that user input is required on an element. We only need to change the code for the Name field.

    Find the code that looks like this:

    <?php if ($req) echo "aria-required='true'"; ?>
    

    and change it to:

     aria-required="true"
    

    We hardcoded the value to true only for the Name variable. For E-mail, the code for this attribute doesn't need changing.

  • For Templates calling comment_form() in comment.php

    These templates lack HTML code for displaying individual input fields. They generate that code by calling WordPress comment_form() function, like so:

    <?php comment_form(); ?>
    

    We will call the same function but provide it with the parameter containing our modified code, by replacing the above code with the following:

    <?php
      $fields =  array(
      'author' =>
        '<p class="comment-form-author"><label for="author">' . __( 'Name', 'domainreference' ).
        '<span class="required"> *</span></label>' .
        '<input id="author" name="author" type="text" value="' . esc_attr( $commenter['comment_author'] ).
        '" size="30" aria-required="true" /></p>',
      'email' =>
        '<p class="comment-form-email"><label for="email">' . __( 'Email', 'domainreference' ).
    	'<span class="required"> (optional)</span></label>' .
        '<input id="email" name="email" type="text" value="' . esc_attr(  $commenter['comment_author_email'] ).
        '" size="30" aria-required="false" /></p>',
      );
      $comments_args = array(
      'fields' => apply_filters( 'comment_form_default_fields', $fields ),
      );  
      comment_form($comments_args);
    ?>
    

    This code uses for name and email fields the default code that is used in comment_form function page on WordPress Codex site, but with slight modifications:

    • Author element in $fields array contains code for Name field and we just hard-coded the required message to it and set aria-required attribute to true.
    • For Email input field, we hardcoded optional message and set aria-required to false.

Issue with missing "awaiting moderation" message

As mentioned at the beginning of the article, by making the email optional, the visitor making the comment will not receive the "Your comment is awaiting moderation." message" even when it is enabled in Dashboard > Settings > Discussion. This can confuse the commentator and might think that the comment was not submitted successfully.

To fix this, we can modify our function that we added earlier for 'preprocess_comment' filter by checking first, if the email field is empty and if it is, insert some dummy email. The lines added are lines 6 and 7 highlighted yellow.

function require_comment_name($fields) {	

if ($fields['comment_author'] == '')
wp_die('Error: please enter a valid name.');

if ($fields['comment_author_email'] == '')
$fields['comment_author_email']='[email protected]';
	
return $fields;
}
add_filter('preprocess_comment', 'require_comment_name');

That way, the visitor making a comment that did not provide their e-mail will have the exact same user experience as the visitor who did provide it.

Conclusion

Comments on blog posts are always welcome. Making an email optional in the Comment Form might be one way to encourage more comments, but in the case of WordPress, you can only have both Name and Email optional. This post demonstrated what you need to do to make only email optional field while leaving Name as required field.

If you found this article useful, consider sharing it on social networks or drop a comment. Don't worry, the email field is not required. 🙂

20 Comments

Click HERE to add your Comment
  1. ban thi nghiem
    January 18, 2016
  2. mehjdi
    August 5, 2017
  3. צלם חתונות
    August 15, 2017
  4. Muhammad Adnan
    July 1, 2018
  5. Deku
    May 22, 2019
  6. GODSPOWER
    June 7, 2019
  7. AdrienP
    September 1, 2019
    • admin
      September 2, 2019
  8. Suzie
    September 7, 2019
  9. lol
    November 24, 2019
    • admin
      November 25, 2019
  10. Felipe
    July 20, 2020
    • admin
      July 20, 2020
  11. Boris
    December 11, 2020
    • admin
      December 11, 2020
      • Boris
        December 12, 2020
        • admin
          December 12, 2020
  12. Hemant Manikpuri
    January 25, 2021
  13. Dominik
    December 2, 2021
    • admin
      December 2, 2021

Write a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.