Using WordPress ‘comment_form_after’ PHP action

The comment_form_after WordPress action fires after the comment form, allowing you to insert custom code.

Usage

add_action('comment_form_after', 'your_function_name');
function your_function_name() {
    // your custom code here
}

Parameters

  • None

More information

See WordPress Developer Resources: comment_form_after

Examples

Add a custom message after the comment form

Displays a thank you message after the comment form.

add_action('comment_form_after', 'display_thank_you_message');
function display_thank_you_message() {
    echo '<p><strong>Thank you for your comment!</strong></p>';
}

Inserts a GDPR consent checkbox after the comment form.

add_action('comment_form_after', 'add_gdpr_consent_checkbox');
function add_gdpr_consent_checkbox() {
    echo '<input type="checkbox" name="gdpr_consent" required> <label>I consent to the storage of my data according to the Privacy Policy</label>';
}

Display a list of recent comments

Shows a list of recent comments below the comment form.

add_action('comment_form_after', 'display_recent_comments');
function display_recent_comments() {
    $recent_comments = get_comments(array('number' => 5, 'status' => 'approve'));
    echo '<h3>Recent Comments</h3>';
    echo '<ul>';
    foreach ($recent_comments as $comment) {
        echo '<li>' . $comment->comment_author . ': ' . $comment->comment_content . '</li>';
    }
    echo '</ul>';
}

Add social sharing buttons

Adds social sharing buttons for Facebook and Twitter after the comment form.

add_action('comment_form_after', 'add_social_sharing_buttons');
function add_social_sharing_buttons() {
    echo '<div class="social-sharing">';
    echo '<a href="https://www.facebook.com/sharer/sharer.php?u=' . urlencode(get_permalink()) . '" target="_blank">Share on Facebook</a>';
    echo '<a href="https://twitter.com/intent/tweet?url=' . urlencode(get_permalink()) . '&text=' . urlencode(get_the_title()) . '" target="_blank">Share on Twitter</a>';
    echo '</div>';
}

Display a custom message if comments are closed

Shows a message if the comments are closed for the current post.

add_action('comment_form_after', 'display_comments_closed_message');
function display_comments_closed_message() {
    if (!comments_open()) {
        echo '<p><strong>Comments are closed for this post.</strong></p>';
    }
}