WordPress : How to make email field not required in comments

Last updated: January 11, 2016
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 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 name field required.

To achieve this, we need to use a WordPress filter and modifying 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!

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 the WordPress will consider 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.

MyThemeShop - Premium WordPress Themes & Plugins
Choose between Blog, Magazine, WooCommerce, Business Themes.
  • Fully Responsive Designs
  • Premium Support 24x7
  • 30 Day Money Back Policy
  • Buy Once, Use for Lifetime
  • SEO Friendly
  • Use On Unlimited Sites
Check it now
This is an affiliate ad.

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.

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 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. As you can see, the email field is not required.

Share this page

4 Comments

Add a Comment

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

Back to Top