The comment_form_top WordPress PHP action fires at the top of the comment form, inside the form tag.
Usage
add_action('comment_form_top', 'your_custom_function'); function your_custom_function() { // your custom code here }
Parameters
- None
More information
See WordPress Developer Resources: comment_form_top
Examples
Add a custom message at the top of the comment form
This example adds a custom message at the top of the comment form.
add_action('comment_form_top', 'add_custom_message'); function add_custom_message() { echo '<p><strong>Note:</strong> Please be respectful and follow our community guidelines when posting your comment.</p>'; }
Add a custom CSS class to the comment form
This example adds a custom CSS class to the comment form.
add_action('comment_form_top', 'add_custom_css_class'); function add_custom_css_class() { echo '<div class="custom-comment-form-class">'; // Remember to close the div in another action. }
Display the current user’s name
This example displays the current user’s name at the top of the comment form.
add_action('comment_form_top', 'display_current_user_name'); function display_current_user_name() { $current_user = wp_get_current_user(); echo '<p>Welcome, ' . esc_html($current_user->display_name) . '!</p>'; }
Display a custom field
This example displays a custom field called “comment_instructions” at the top of the comment form.
add_action('comment_form_top', 'display_custom_field'); function display_custom_field() { $instructions = get_post_meta(get_the_ID(), 'comment_instructions', true); if ($instructions) { echo '<p>' . esc_html($instructions) . '</p>'; } }
Display comment count
This example displays the total number of comments for the current post.
add_action('comment_form_top', 'display_comment_count'); function display_comment_count() { $comments_count = get_comments_number(); echo '<p>There are ' . $comments_count . ' comments on this post.</p>'; }