Placeholders in Comment Form Fields
Adds helpful placeholder text to the comment, name, email and website fields of the comment form while keeping the visible labels for accessibility.
Opens WPIDE on
Code
$wpide_comment_placeholders = array(
'comment' => 'Share your thoughts…',
'author' => 'Your name',
'email' => '[email protected] (never published)',
'url' => 'https://your-site.com (optional)',
);
$wpide_add_placeholder = function ( $html, $name ) use ( $wpide_comment_placeholders ) {
if ( empty( $wpide_comment_placeholders[ $name ] ) || false !== strpos( $html, 'placeholder=' ) ) {
return $html;
}
return preg_replace( '/<(input|textarea)\b/', '<$1 placeholder="' . esc_attr( $wpide_comment_placeholders[ $name ] ) . '"', $html, 1 );
};
add_filter( 'comment_form_default_fields', function ( $fields ) use ( $wpide_add_placeholder ) {
foreach ( array( 'author', 'email', 'url' ) as $name ) {
if ( isset( $fields[ $name ] ) ) {
$fields[ $name ] = $wpide_add_placeholder( $fields[ $name ], $name );
}
}
return $fields;
} );
add_filter( 'comment_form_defaults', function ( $defaults ) use ( $wpide_add_placeholder ) {
$defaults['comment_field'] = $wpide_add_placeholder( $defaults['comment_field'], 'comment' );
return $defaults;
} );
Notes
Edit the texts at the top; placeholders never replace labels, so screen readers still announce each field.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.