Last active
August 9, 2018 15:40
-
-
Save BenSibley/5fafc901c32a0be7b6645174d083c143 to your computer and use it in GitHub Desktop.
How to Add HTML5 Placeholders to the WordPress Comment Form
This file contains 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
function my_update_comment_fields( $fields ) { | |
$commenter = wp_get_current_commenter(); | |
$req = get_option( 'require_name_email' ); | |
$label = $req ? '*' : ' ' . __( '(optional)', 'text-domain' ); | |
$aria_req = $req ? "aria-required='true'" : ''; | |
$fields['author'] = | |
'<p class="comment-form-author"> | |
<label for="author">' . __( "Name", "text-domain" ) . $label . '</label> | |
<input id="author" name="author" type="text" placeholder="' . esc_attr__( "Jane Doe", "text-domain" ) . '" value="' . esc_attr( $commenter['comment_author'] ) . | |
'" size="30" ' . $aria_req . ' /> | |
</p>'; | |
$fields['email'] = | |
'<p class="comment-form-email"> | |
<label for="email">' . __( "Email", "text-domain" ) . $label . '</label> | |
<input id="email" name="email" type="email" placeholder="' . esc_attr__( "name@email.com", "text-domain" ) . '" value="' . esc_attr( $commenter['comment_author_email'] ) . | |
'" size="30" ' . $aria_req . ' /> | |
</p>'; | |
$fields['url'] = | |
'<p class="comment-form-url"> | |
<label for="url">' . __( "Website", "text-domain" ) . '</label> | |
<input id="url" name="url" type="url" placeholder="' . esc_attr__( "http://google.com", "text-domain" ) . '" value="' . esc_attr( $commenter['comment_author_url'] ) . | |
'" size="30" /> | |
</p>'; | |
return $fields; | |
} | |
add_filter( 'comment_form_default_fields', 'my_update_comment_fields' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I know this piece of code is 2y old, but just a warning to potential users of the snippet: Be sure to include the 'cookies' checkbox input that's added with the latest GDPR WordPress update v 4.9.5