Using WordPress ‘comment_form_default_fields’ PHP filter

The comment_form_default_fields WordPress PHP filter allows you to modify the default comment form fields.

Usage

add_filter('comment_form_default_fields', 'my_custom_comment_form_fields');
function my_custom_comment_form_fields($fields) {
    // your custom code here
    return $fields;
}

Parameters

  • $fields (array): An associative array containing the default comment fields.

More information

See WordPress Developer Resources: comment_form_default_fields

Examples

Change the label for the “Name” field

Modify the label for the “Name” field in the comment form.

add_filter('comment_form_default_fields', 'change_name_field_label');
function change_name_field_label($fields) {
    $fields['author'] = str_replace('Name', 'Your Name', $fields['author']);
    return $fields;
}

Add a placeholder to the “Email” field

Add a placeholder text to the “Email” field in the comment form.

add_filter('comment_form_default_fields', 'add_email_field_placeholder');
function add_email_field_placeholder($fields) {
    $fields['email'] = str_replace('type="email"', 'type="email" placeholder="Email Address"', $fields['email']);
    return $fields;
}

Remove the “Website” field

Remove the “Website” field from the comment form.

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

Make ‘comment’ field optional

// Function to modify the comment form fields
function make_comment_field_optional($fields)
{
    // Set the 'comment' field as optional
    $fields['comment']['required'] = false;

    return $fields;
}

// Hook the function to the 'comment_form_default_fields' filter
add_filter('comment_form_default_fields', 'make_comment_field_optional');

Remove ’email’ field from comment form

// Function to modify the comment form fields
function hide_email_field($fields)
{
    // Remove the 'email' field from the comment form
    unset($fields['email']);

    return $fields;
}

// Hook the function to the 'comment_form_default_fields' filter
add_filter('comment_form_default_fields', 'hide_email_field');

Remove ‘author’ (name) field from comment form

// Function to modify the default comment form fields
function hide_author_field_in_comment_form($fields)
{
    // Check if the 'author' field exists in the fields array
    if (isset($fields['author'])) {
        // Remove the 'author' field from the fields array
        unset($fields['author']);
    }

    return $fields;
}

// Hook our function to the comment_form_default_fields filter
add_filter('comment_form_default_fields', 'hide_author_field_in_comment_form');