Using WordPress ‘comment_loop_start’ PHP action

The comment_loop_start WordPress PHP action fires once the comment loop is started.

Usage

add_action('comment_loop_start', 'your_custom_function');

function your_custom_function() {
    // your custom code here
}

Parameters

  • None

More information

See WordPress Developer Resources: comment_loop_start

Examples

Display a message before comments

Display a custom message before the comment section starts on your website.

add_action('comment_loop_start', 'display_message_before_comments');

function display_message_before_comments() {
    echo '<p><strong>Join the conversation:</strong> Share your thoughts below!</p>';
}

Add a custom class to the comments container

Add a custom CSS class to the comments container.

add_action('comment_loop_start', 'add_custom_class_to_comments_container');

function add_custom_class_to_comments_container() {
    echo '<div class="custom-comments-container">';
}

add_action('comment_loop_end', 'close_custom_class_div');

function close_custom_class_div() {
    echo '</div>';
}

Show total comments count

Display the total number of comments on a post before the comment section.

add_action('comment_loop_start', 'show_total_comments_count');

function show_total_comments_count() {
    echo '<p>Total comments: ' . get_comments_number() . '</p>';
}

Display a message for logged-in users

Show a custom message for logged-in users before the comment section.

add_action('comment_loop_start', 'show_message_for_logged_in_users');

function show_message_for_logged_in_users() {
    if (is_user_logged_in()) {
        echo '<p>Welcome back, ' . wp_get_current_user()->display_name . '! Share your thoughts below.</p>';
    }
}

Add a separator between the post content and comments section

Insert a horizontal line between the post content and comments section.

add_action('comment_loop_start', 'add_separator_before_comments');

function add_separator_before_comments() {
    echo '<hr>';
}