Last updated on September 25, 2023

Change the Default Comment Form Fields

Don’t know where to add this snippet? Read our guide: How to add code snippets.

Modify comment form fields.

The default comment form fields in WordPress can be customized by using the comment_form_default_fields filter. This allows you to add, remove, or modify the default fields that are displayed in the comment form.

Here’s an example code snippet that demonstrates how to change the default comment form fields:

function wpsnippets_custom_comment_fields($fields) {
    // Remove the existing fields
    unset($fields['author']);
    unset($fields['email']);
    unset($fields['url']);

    // Add custom fields
    $fields['name'] = '<p class="comment-form-name">' .
        '<label for="author">' . __('Name') . '</label>' .
        '<input id="author" name="author" type="text" value="' . esc_attr($commenter['comment_author']) . '" size="30" /></p>';

    $fields['email'] = '<p class="comment-form-email">' .
        '<label for="email">' . __('Email') . '</label>' .
        '<input id="email" name="email" type="email" value="' . esc_attr($commenter['comment_author_email']) . '" size="30" /></p>';

    $fields['website'] = '<p class="comment-form-website">' .
        '<label for="url">' . __('Website') . '</label>' .
        '<input id="url" name="url" type="url" value="' . esc_attr($commenter['comment_author_url']) . '" size="30" /></p>';

    return $fields;
}
add_filter('comment_form_default_fields', 'wpsnippets_custom_comment_fields');

In this example, we first remove the default fields (author, email, and url) by unsetting them from the $fields array. Then, we add our custom fields (name, email, and website) by appending them to the $fields array with the desired HTML markup.

You can modify this code snippet to suit your specific needs by adding or removing fields as required.

Examples

Example 1: Change the default comment form fields by removing the website field

This example demonstrates how to modify the default comment form fields in WordPress by removing the website field.

function wpsnippets_remove_website_field( $fields ) {
    if ( isset( $fields['url'] ) ) {
        unset( $fields['url'] );
    }
    return $fields;
}
add_filter( 'comment_form_default_fields', 'wpsnippets_remove_website_field' );

The code above uses the comment_form_default_fields filter to modify the default comment form fields. It checks if the website field (url) exists in the $fields array and removes it using the unset() function. The modified $fields array is then returned.

Example 2: Change the default comment form fields by adding a custom field

This example demonstrates how to add a custom field to the default comment form fields in WordPress.

function wpsnippets_add_custom_field( $fields ) {
    $fields['custom_field'] = '<p class="comment-form-custom-field"><label for="custom_field">' . __( 'Custom Field' ) . '</label><input id="custom_field" name="custom_field" type="text" value="" /></p>';
    return $fields;
}
add_filter( 'comment_form_default_fields', 'wpsnippets_add_custom_field' );

The code above uses the comment_form_default_fields filter to modify the default comment form fields. It adds a new field named custom_field to the $fields array, which includes a label and an input field. The modified $fields array is then returned.

Example 3: Change the default comment form fields by modifying an existing field

This example demonstrates how to modify an existing field in the default comment form fields in WordPress.

function wpsnippets_modify_comment_field( $fields ) {
    $fields['comment_field'] = str_replace( '<textarea', '<textarea placeholder="Your comment"', $fields['comment_field'] );
    return $fields;
}
add_filter( 'comment_form_default_fields', 'wpsnippets_modify_comment_field' );

The code above uses the comment_form_default_fields filter to modify the default comment form fields. It targets the comment_field key in the $fields array and uses the str_replace() function to add a placeholder attribute to the textarea field. The modified $fields array is then returned.

Last updated on September 25, 2023. Originally posted on October 5, 2023.

Leave a Reply