How to add more fields in comments section without a plugin?

I need more information from the user in the comment section, like mobile number, states etc. Fields might be a text area or dropdown.

Here is an example to customize your contact form. This needed to be added in function.php

function new_comment_form_default_fields( $fields ) {
    $commenter     = wp_get_current_commenter();
    $user          = wp_get_current_user();
    $user_identity = $user->exists() ? $user->display_name : '';
    $req           = get_option( 'require_name_email' );
    $aria_req      = ( $req ? " aria-required='true'" : '' );
    $html_req      = ( $req ? " required='required'" : '' );
    $html5         = current_theme_supports( 'html5', 'comment-form' ) ? 'html5' : false;

    $fields = [
		'comment_field' => '<p class="comment-form-comment"><textarea id="comment" name="comment" placeholder="Write a Comment" cols="45" rows="3" maxlength="65525" aria-required="true" required="required"></textarea></p>',
        'author' => '<p class="comment-form-author"><input id="author" name="author" type="text" placeholder="Name" value="' . esc_attr( $commenter['comment_author'] ) . '" size="30" maxlength="245"' . $aria_req . $html_req . ' /></p>',
        'email'  => '<p class="comment-form-email"><input id="email" name="email" placeholder="Email" ' . ( $html5 ? 'type="email"' : 'type="text"' ) . ' value="' . esc_attr(  $commenter['comment_author_email'] ) . '" size="30" maxlength="100" aria-describedby="email-notes"' . $aria_req . $html_req  . ' /></p>',
         ];

    return $fields;
}
add_filter( 'comment_form_default_fields', 'new_comment_form_default_fields' );
1 Like

Thanku

Thanks for your precious suggestion. This is very helpful to us. I agree with this.