Customizing the WordPress Comment Form with PHP

Luca B Dec 31, 2024 WordPress Customization
How can I change the comment box on my blog so it looks different?
What is the PHP code to customize the WordPress comment form fields and their order in the comments.php template?
Andy answered Dec 31, 2024

Customizing the WordPress Comment Form

Custom Code Solution

You can customize the comment form by using the comment_form_default_fields and comment_form_defaults filters in your theme's functions.php file.

Change the default comment form fields (name, email, website):

function modify_comment_form_fields($fields) {
    // Remove website field
    unset($fields['url']);
    
    // Modify name field
    $fields['author'] = '<p class="comment-form-author">' . 
        '<label for="author">Name *</label>' .
        '<input id="author" name="author" type="text" required="required" />' .
        '</p>';
    
    // Modify email field
    $fields['email'] = '<p class="comment-form-email">' .
        '<label for="email">Email *</label>' .
        '<input id="email" name="email" type="email" required="required" />' .
        '</p>';
    
    return $fields;
}
add_filter('comment_form_default_fields', 'modify_comment_form_fields');

Customize the main comment textarea and form settings:

function modify_comment_form($defaults) {
    $defaults['title_reply'] = 'Leave a Comment';
    $defaults['comment_field'] = '<p class="comment-form-comment">' .
        '<label for="comment">Your Message *</label>' .
        '<textarea id="comment" name="comment" rows="8" required="required"></textarea>' .
        '</p>';
    $defaults['label_submit'] = 'Post Comment';
    $defaults['comment_notes_before'] = '';
    $defaults['comment_notes_after'] = '';
    
    return $defaults;
}
add_filter('comment_form_defaults', 'modify_comment_form');

Add custom CSS styling (add to your theme's style.css):

.comment-form-author,
.comment-form-email,
.comment-form-comment {
    margin-bottom: 20px;
}

.comment-form input[type="text"],
.comment-form input[type="email"],
.comment-form textarea {
    width: 100%;
    padding: 10px;
    border: 1px solid #ddd;
    border-radius: 4px;
}

.form-submit input[type="submit"] {
    background: #333;
    color: #fff;
    padding: 10px 20px;
    border: none;
    border-radius: 4px;
    cursor: pointer;
}

Plugin Alternative

If you prefer a plugin solution, consider:

  1. wpDiscuz - Advanced comment system with custom forms
  2. Comments – wpForo - Modern comment layouts and styling

Notes

  • Place the PHP code in your theme's functions.php file
  • Place CSS in your theme's style.css file or in the WordPress Customizer
  • The code is adaptable - modify field labels, classes, and HTML structure as needed
  • Use WordPress's built-in comment_form() function in your template files
  • Test thoroughly after implementation to ensure proper form submission

For advanced validation or additional fields, you'll need to use the preprocess_comment hook to handle the form submission.