Archived topic
Add placeholder to comment textarea field
10 replies · Started by George on October 28, 2020
Is there a function to add a placeholder in the comment form textarea field?
The following code doesn't work:
add_filter( 'comment_form_defaults', 'gm_textarea_placeholder' );
function gm_textarea_placeholder( $fields ) {
$fields['comment_field'] = str_replace(
'<textarea',
'<textarea placeholder="Comment"',
$fields['comment_field']
);
return $fields;
}
Hi George,
The comment is field is handled by WordPress and I found this article that might help?
https://pupungbp.com/add-placeholder-to-your-comment-form-in-wordpress/
Yes, Leo, I have tried everything, nothing works!
Any chance you can check with WordPress' support team first and see if they can provide any quick pointers?
Oh really? Not hoping to get a reply any time soon from them. Damn, didn't think it was that complicated! Maybe somehow GP overrides those settings? I have tried a lot of code that should work but it doesn't.
I don't think so but definitely could be wrong :)
Did you test if the same code works in a twenty series WP theme?
Let me know :)
Doesn't work on a default theme, either. The yourdomain needs to be placed by generatepress, right?
I have also tried this, no luck!
https://www.billerickson.net/code/use-placeholders-instead-of-labels-in-comment-form/
What about this:
add_filter( 'comment_form_defaults', function( $defaults ) {
$defaults['comment_field'] = sprintf(
'<p class="comment-form-comment"><label for="comment" class="screen-reader-text">%1$s</label><textarea placeholder="PLACE HOLDER TEXT HERE" id="comment" name="comment" cols="45" rows="8" aria-required="true"></textarea></p>',
esc_html__( 'Comment', 'generatepress' )
);
return $defaults;
}, 20 );
Yes, that's it, it was the priority!
This one also works now:
function placeholder_comment_form_field($fields) {
$replace_comment = __('Your Comment', 'generatepress');
$fields['comment_field'] = '<p class="comment-form-comment"><label for="comment">' . _x( 'Comment', 'noun' ) .
'</label><textarea id="comment" name="comment" cols="45" rows="8" placeholder="'.$replace_comment.'" aria-required="true"></textarea></p>';
return $fields;
}
add_filter( 'comment_form_defaults', 'placeholder_comment_form_field', 20 );
Leo, you are a savior, thank you!
No problem :)