Skip to content

Instantly share code, notes, and snippets.

@ivanildodias
Last active July 30, 2019 07:56
Show Gist options
  • Save ivanildodias/436c6083175cb4b2ba8a to your computer and use it in GitHub Desktop.
Save ivanildodias/436c6083175cb4b2ba8a to your computer and use it in GitHub Desktop.
Remove "novalidate" attribute in default WordPress Comment Form
ob_start();
$commenter = wp_get_current_commenter();
$req = get_option( 'require_name_email' );
$aria_req = ( $req ? " aria-required='true'" : '' );
$html_req = ( $req ? " required='required'" : '' );
$span_req = ( $req ? ' <span class="required">*</span>' : '' );
comment_form( array(
'title_reply' => __( 'Leave your thoughts', 'issimple' ),
'comment_notes_after' => '',
'fields' => apply_filters( 'comment_form_default_fields', array(
'author' => '<div class="comment-form-author form-group">' . '<label for="author">' . __( 'Name', 'issimple' ) . $span_req . '</label> ' .
'<input id="author" class="form-control" name="author" type="text" value="' . esc_attr( $commenter['comment_author'] ) . '" size="40"' . $aria_req . $html_req . ' /></div>',
'email' => '<div class="comment-form-email form-group"><label for="email">' . __( 'Email', 'issimple' ) . $span_req . '</label> ' .
'<input id="email" class="form-control" name="email" type="email" value="' . esc_attr( $commenter['comment_author_email'] ) . '" size="40" aria-describedby="email-notes"' . $aria_req . $html_req . ' /></div>',
'url' => '<div class="comment-form-url form-group"><label for="url">' . __( 'Website', 'issimple' ) . '</label> ' .
'<input id="url" class="form-control" name="url" ' . ( $html5 ? 'type="url"' : 'type="text"' ) . ' value="' . esc_attr( $commenter['comment_author_url'] ) . '" size="40" /></div>',
) ),
'comment_field' => '<div class="comment-form-comment form-group"><label for="comment">' . __( 'Comment', 'issimple' ) . ' <span class="required">*</span></label> ' .
'<textarea id="comment" class="form-control" name="comment" cols="45" rows="8" aria-describedby="form-allowed-tags" aria-required="true" required="required"></textarea></div>',
'class_submit' => 'submit btn btn-default',
) );
$comment_form = ob_get_contents();
ob_end_clean();
echo str_replace( 'novalidate', '', $comment_form );
@DediData
Copy link

Use this function rather than comment_form()

function validate_comment_form(){
    ob_start();
    comment_form();
    echo str_replace('novalidate','data-toggle="validator" ',ob_get_clean());
}

@hmaesta
Copy link

hmaesta commented Aug 15, 2018

You can remove novalidate attribute with this simple Javascript:

<script type="text/javascript">
    var commentForm = document.getElementById('commentform'); 
    commentForm.removeAttribute('novalidate');
</script>

No jQuery needed.

You can include the following code to run script just on posts:

footer.php

<?php if( is_singular() ) : ?>
    <script type="text/javascript">
        var commentForm = document.getElementById('commentform');
        commentForm.removeAttribute('novalidate');
    </script>
<?php endif; ?>

@smarteist
Copy link

You can remove html 5 support just before printing out your comments form

        // removes "novalidate" attribute from default output form
        remove_theme_support('html5');

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment