How to Add Input Fields to the Comment Form in WordPress

Home » Snippets » How to Add Input Fields to the Comment Form in WordPress
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

Looking to customize the WordPress comment form for your website? Adding input fields to the comment form is a great way to collect additional information from your visitors or to enhance engagement on your site. In this article, we’ll show you step-by-step how to add input fields to the comment form in WordPress. Whether you want to ask for a visitor’s location or give them the option to subscribe to your newsletter, this guide will help you do it. Let’s dive in!

					function wpturbo_add_comment_fields( $fields ) {
    $fields['wpturbo_comment_subject'] = '<p class="comment-form-subject"><label for="subject">' . __( 'Subject', 'wpturbo' ) . '</label>' .
                                        '<input id="subject" name="subject" type="text" size="30" /></p>';
    $fields['wpturbo_comment_rating'] = '<p class="comment-form-rating"><label for="rating">' . __( 'Rating', 'wpturbo' ) . '</label>' .
                                        '<select id="rating" name="rating">
                                            <option value="5">' . __( '5 Stars', 'wpturbo' ) . '</option>
                                            <option value="4">' . __( '4 Stars', 'wpturbo' ) . '</option>
                                            <option value="3">' . __( '3 Stars', 'wpturbo' ) . '</option>
                                            <option value="2">' . __( '2 Stars', 'wpturbo' ) . '</option>
                                            <option value="1">' . __( '1 Star', 'wpturbo' ) . '</option>
                                        </select></p>';
    return $fields;
}
add_filter( 'comment_form_default_fields', 'wpturbo_add_comment_fields' );
				

If you want to add additional input fields to the default comment form in WordPress, this code snippet is perfect for you.

The first part of the code creates a new function called wpturbo_add_comment_fields(). This function will be responsible for adding two new input fields to the default comment form: a Subject field and a Rating field.

The function takes in the $fields variable as a parameter, which contains the default fields for the comment form, and then returns these fields with the new ones included.

Inside the function, we add the Subject field to $fields using the following code:

$fields['wpturbo_comment_subject'] = '<p class="comment-form-subject"><label for="subject">' . __( 'Subject', 'wpturbo' ) . '</label>' .
                                        '<input id="subject" name="subject" type="text" size="30" /></p>';

In the above code snippet we’ve added the label, which has an ID of subject, and an input field with a name of subject. This field will give the user the ability to enter a subject for their comment.

Next, we add the Rating field to $fields using this code snippet:

$fields['wpturbo_comment_rating'] = '<p class="comment-form-rating"><label for="rating">' . __( 'Rating', 'wpturbo' ) . '</label>' .
                                        '<select id="rating" name="rating">
                                            <option value="5">' . __( '5 Stars', 'wpturbo' ) . '</option>
                                            <option value="4">' . __( '4 Stars', 'wpturbo' ) . '</option>
                                            <option value="3">' . __( '3 Stars', 'wpturbo' ) . '</option>
                                            <option value="2">' . __( '2 Stars', 'wpturbo' ) . '</option>
                                            <option value="1">' . __( '1 Star', 'wpturbo' ) . '</option>
                                        </select></p>';

The code above creates a Rating field with a dropdown that has 5-star rating options. We used the ‘select’ tag to create the dropdown and we’ve linked it up with label, which has an ID of rating.

Finally, we use the add_filter() function to hook the wpturbo_add_comment_fields() function into the comment_form_default_fields filter. This ensures that both the Subject and Rating fields are added to the default comment form.

Register an account to save your snippets or go Pro to get more features.