Using WordPress ‘comment_form’ PHP action

The comment_form WordPress PHP action fires at the bottom of the comment form, inside the closing form tag.

Usage

add_action('comment_form', 'your_custom_function_name');
function your_custom_function_name($post_id) {
  // your custom code here
}

Parameters

  • $post_id (int): The post ID.

More information

See WordPress Developer Resources: comment_form

Examples

Add a Custom Field to the Comment Form

This example adds a custom field “Favorite Color” to the comment form.

add_action('comment_form', 'add_favorite_color_field');
function add_favorite_color_field($post_id) {
  echo '<p class="comment-form-color"><label for="color">' . __('Favorite Color') . '</label><input id="color" name="color" type="text" /></p>';
}

Add a Checkbox to Agree to the Terms and Conditions

This example adds a checkbox for users to agree to terms and conditions before submitting a comment.

add_action('comment_form', 'add_terms_and_conditions_checkbox');
function add_terms_and_conditions_checkbox($post_id) {
  echo '<p class="comment-form-tac"><input id="tac" name="tac" type="checkbox" /><label for="tac">' . __('I agree to the Terms and Conditions') . '</label></p>';
}