Last active
May 29, 2022 17:03
-
-
Save jsit/8a6547b83c8c0bbe2e07f87faa056b71 to your computer and use it in GitHub Desktop.
Customize WordPress comment form output
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Rewrite the HTML for the author, email, and url comment fields | |
*/ | |
function custom_comment_fields( $fields ) { | |
// https://codex.wordpress.org/Function_Reference/comment_form | |
$commenter = wp_get_current_commenter(); | |
$req = get_option( 'require_name_email' ); | |
$aria_req = ( $req ? " aria-required='true'" : '' ); | |
$html_req = ( $req ? " required='required'" : '' ); | |
$html5 = current_theme_supports( 'html5', 'comment-form' ); | |
$fields['author'] = '<p class="comment-form-author">' . '<label for="author">' . __( 'Name' ) . ( $req ? ' <span class="required">*</span>' : '' ) . '</label> ' . '<input id="author" name="author" type="text" value="' . esc_attr( $commenter['comment_author'] ) . '" size="30" maxlength="245"' . $aria_req . $html_req . ' /></p>'; | |
$fields['email'] = '<p class="comment-form-email"><label for="email">' . __( 'Email' ) . ( $req ? ' <span class="required">*</span>' : '' ) . '</label> ' . '<input id="email" name="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>'; | |
$fields['url'] = '<p class="comment-form-url"><label for="url">' . __( 'Website' ) . '</label> ' . '<input id="url" name="url" ' . ( $html5 ? 'type="url"' : 'type="text"' ) . ' value="' . esc_attr( $commenter['comment_author_url'] ) . '" size="30" maxlength="200" /></p>'; | |
return $fields; | |
} | |
add_filter( 'comment_form_default_fields', 'custom_comment_fields' ); | |
/** | |
* Rewrite the HTML for the comment textarea field | |
*/ | |
function custom_comment_form_defaults( $defaults ) { | |
// https://codex.wordpress.org/Function_Reference/comment_form | |
$defaults['comment_field'] = '<p class="comment-form-comment"><label for="comment">' . _x( 'Comment', 'noun' ) . '</label><textarea id="comment" name="comment" cols="45" rows="8" aria-required="true"></textarea></p>'; | |
return $defaults; | |
} | |
add_filter( 'comment_form_defaults', 'custom_comment_form_defaults' ); | |
/** | |
* Re-order the comment fields that get displayed | |
*/ | |
function reorder_comment_form_fields( $comment_fields ) { | |
$new_comment_fields = array( | |
'author' => $comment_fields['author'], | |
'email' => $comment_fields['email'], | |
'url' => $comment_fields['url'], | |
'comment' => $comment_fields['comment'] | |
); | |
return $new_comment_fields; | |
} | |
add_filter ( 'comment_form_fields', 'reorder_comment_form_fields' ); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@jsit great work, brother. But the required fields are not working. Would you fix it?