Skip to content

Instantly share code, notes, and snippets.

@BenSibley
Last active August 9, 2018 15:40
Show Gist options
  • Save BenSibley/5fafc901c32a0be7b6645174d083c143 to your computer and use it in GitHub Desktop.
Save BenSibley/5fafc901c32a0be7b6645174d083c143 to your computer and use it in GitHub Desktop.
How to Add HTML5 Placeholders to the WordPress Comment Form
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' );
@IgorMilosavljevic
Copy link

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

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