Post comment form customization
Post comment forms are not the easiest to customize. Thankfully, GeneratePress provides a few functions to facilitate the process.
Resize author comment avatar
The following snippet will increase the size of the author comment avatar:
add_filter( 'generate_comment_avatar_size', function() {
return 100; // The size in pixels. This would be 100 x 100.
} );
Rename comment title
The following snippet will rename the comment title from [comment count] thoughts on... to Comments (see next screenshot):
add_filter( 'generate_comment_form_title', function() {
return 'Comments';
} );
Add content below comment title
This snippet will add content below the post title:
add_action( 'generate_below_comments_title','generate_comments_email_message' );
function generate_comments_email_message()
{
?>
<p class="total-comments">Total Comments: ( <?php comments_popup_link( __( 'Leave a comment', 'generatepress' ), __( '1', 'generatepress' ), __( '%', 'generatepress' ) ); ?> ) </p>
<?php
}
Add placeholder text in comment textarea field
This snippet will add a placeholder text in comment textarea field (see next screenshot):
function placeholder_comment_form_field($fields) {
$replace_comment = __('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 );
Add text after Leave Comment title
This snippet will insert content after the Leave Comment title:
add_filter('comment_form_defaults', 'change_note_after_comment_form',99);
function change_note_after_comment_form($defaults) {
$defaults['comment_notes_before'] = '<p class="comment-notes"> Your email address will not be published. Required fields are marked* </p>';
return $defaults;
}
Alternatively, create a block element, assign it to appear to all single posts, select Hook as ELEMENT TYPE, Custom Hook as HOOK NAME and enter comment_form_top as CUSTOM HOOK NAME. Then build your block that should appear underneath the Leave Comment title.
Add comment URL label without removing the placeholder
This snippet will add a URL label without removing the placeholder:
add_filter( 'comment_form_default_fields', function( $fields ) {
$commenter = wp_get_current_commenter();
$fields['url'] = sprintf(
'<label for="url">%1$s</label><input placeholder="%1$s" id="url" name="url" type="url" value="%2$s" size="30" />',
esc_html__( 'Website', 'generatepress' ),
esc_attr( $commenter['comment_author_url'] )
);
return $fields;
}, 20 );
That’s all for now, I will updating this post with new related code snippets in the future!
Remove E-mail and URL field from Comment Form
If you would like to remove e-mail and URL field from comment form, first uncheck the requirement in Settings > Discussion:
Then add the PHP snippet below to remove the fields
add_action( 'after_setup_theme', 'tu_add_comment_url_filter' );
function tu_add_comment_url_filter() {
add_filter( 'comment_form_default_fields', 'tu_disable_comment_url', 20 );
}
function tu_disable_comment_url($fields) {
unset($fields['url']);
unset($fields['email']);
return $fields;
}
The snippet for ‘Add text after Leave Comment title’ is missing from your article…
Thanks for this, I’ve updated the article accordingly!
Thank you for sharing these valuable theme builder tips for beginners! They provide great insights and practical advice that make the customization process much easier to grasp. I appreciate the clarity and detail you’ve included—it’s a huge help for someone just starting out!
Thanks, glad you like the content!
Hi. to remove the URL option (since spammers can leave a dofollow) add this:
.comment-form #url{
display:none;
}
if the email given is associated with a WP account, the name/gravatar is linked to their website. antispam. but genuine commenter friendly
Thanks for your comment. An even better way is as described in this article. I’ve also updated my article to include this method.