apply_filters( ‘comment_class’, string[] $classes, string[] $css_class, string $comment_id, WP_Comment $comment, int|WP_Post $post )

Filters the returned CSS classes for the current comment.

Parameters

$classesstring[]
An array of comment classes.
$css_classstring[]
An array of additional classes added to the list.
$comment_idstring
The comment ID as a numeric string.
$commentWP_Comment
The comment object.
$postint|WP_Post
The post ID or WP_Post object.

Source

return apply_filters( 'comment_class', $classes, $css_class, $comment->comment_ID, $comment, $post );

Changelog

VersionDescription
2.7.0Introduced.

User Contributed Notes

  1. Skip to note 2 content

    we will walk through how to use apply_filters with comment_class to customize the CSS classes for comments in WordPress.

    Examples One

    function wpdocs_comment_class_filter1( $classes, $class, $comment_id, $comment ) {
        // For example, add a custom class to comments by the post author
        if ( $comment->user_id === $post->post_author ) {
            $classes[] = 'comment-by-post-author';
        }
        return $classes;
    }
    add_filter( 'comment_class', 'wpdocs_comment_class_filter1', 10, 4 );

    Examples Two

    function wpdocs_comment_class_filter2( $classes, $class, $comment_id, $comment ) {
        // Add a custom class to comments by users with a specific email domain
        if ( strpos( $comment->comment_author_email, '@example.com' ) !== false ) {
            $classes[] = 'comment-by-example-domain';
        }
        return $classes;
    }
    
    add_filter( 'comment_class', 'wpdocs_comment_class_filter2', 10, 4 );

You must log in before being able to contribute a note or feedback.