Select Page

Do you want to add additional fields to the comment form on your WordPress website? This will allow you to store additional data about your commenters.

Place this code in your theme’s functions.php file or create a custom plugin with this code:

/**
 * Custom function to add new input fields to the comment form
 */
function add_new_input_fields_to_comment_form() {
  // Add a new text field to the comment form
  echo '<p class="comment-form-new-field"><label for="new-field">New Field</label><br /><input id="new-field" name="new-field" type="text" value="" size="30" /></p>';

  // Add a new textarea field to the comment form
  echo '<p class="comment-form-new-field"><label for="new-textarea">New Textarea</label><br /><textarea id="new-textarea" name="new-textarea" cols="45" rows="8"></textarea></p>';
}
add_action( 'comment_form_logged_in_after', 'add_new_input_fields_to_comment_form' );
add_action( 'comment_form_after_fields', 'add_new_input_fields_to_comment_form' );

The ‘add_new_input_fields_to_comment_form’ function is a custom function that does not take any arguments.

New input fields are added to the comment form inside the function using the ‘echo’ command. The first is a text field with the ‘new-field’ ID and name, and the second is a textarea field with the ‘new-textarea’ ID and name.

Finally, the function is hooked to the ‘comment_form_logged_in_after’ and ‘comment_form_after_fields’ actions using the ‘add_action’ function, which adds the new input fields to the comment form.

This code will add new input fields to the comment form on your WordPress site. You can modify the code to add different types of input fields or customize the appearance and behavior of the input fields as desired. Please note that you should use caution when modifying the comment form, as it can create security vulnerabilities if used improperly.

As you might have noticed, the above code only displays the new input fields. To save their contents when the form is submitted, you’ll need to add this additional code to your website:

/**
 * Custom function to save the new input fields added to the comment form to the comment data
 *
 * @param array $commentdata An array of comment data
 *
 * @return array $commentdata The modified array of comment data
 */
function save_new_input_fields_to_comment( $commentdata ) {
  // Save the value of the new text field to the comment data
  if ( isset( $_POST['new-field'] ) ) {
    $commentdata['comment_new_field'] = sanitize_text_field( $_POST['new-field'] );
  }

  // Save the value of the new textarea field to the comment data
  if ( isset( $_POST['new-textarea'] ) ) {
    $commentdata['comment_new_textarea'] = sanitize_textarea_field( $_POST['new-textarea'] );
  }

  return $commentdata;
}
add_filter( 'preprocess_comment', 'save_new_input_fields_to_comment', 10, 1 );

This code is a custom function that saves the values of new input fields added to the comment form in WordPress to the comment data. The function takes one argument: $commentdata, an array of comment data.

Inside the function, the values of the new input fields are saved to the comment data using the ‘$_POST’ global variable and the ‘sanitize_text_field’ and ‘sanitize_textarea_field’ functions. The ‘$_POST’ global variable holds the data submitted via the comment form, and the ‘sanitize_text_field’ and ‘sanitize_textarea_field’ functions are used to sanitize the data to prevent security vulnerabilities.

The ‘if’ statements check if the new input fields are set in the ‘$_POST’ global variable. The values are saved to the comment data using the ‘$commentdata’ array if they are set. The array’s keys are ‘comment_new_field’ and ‘comment_new_textarea’, and the values are the sanitized values of the new input fields.

Finally, the function is hooked to the ‘preprocess_comment’ filter using the ‘add_filter’ function, which applies the changes to the comment data before storing it in the database.

This code will save the new input fields added to the comment form in WordPress to the comment data. You can modify the code to save different types of input fields or customize the way the input field data is saved as desired.