comment_class
Allows you to change the list of CSS classes applied to each comment <li> tag in the template.
Applies when the HTML wrapper for a comment is generated in the template. This allows classes assigned to the comment element to be added, changed, or removed.
It can be used in a theme file (for example, comments.php) where comment_class() is called.
Usage
add_filter( 'comment_class', 'wp_kama_comment_class_filter', 10, 5 );
/**
* Function for `comment_class` filter-hook.
*
* @param string[] $classes An array of comment classes.
* @param string[] $css_class An array of additional classes added to the list.
* @param string $comment_id The comment ID as a numeric string.
* @param WP_Comment $comment The comment object.
* @param int|WP_Post $post The post ID or WP_Post object.
*
* @return string[]
*/
function wp_kama_comment_class_filter( $classes, $css_class, $comment_id, $comment, $post ){
// filter...
return $classes;
}
- $classes(array)
- An array of CSS classes applied to the comment tag.
- $css_class(array)
- An array of additional CSS classes applied to the comment tag — the first parameter of get_comment_class().
- $comment_id(string)
- The comment ID. An integer represented as a string.
- $comment(WP_Comment)
- The current comment object.
- $post(int|WP_Post)
- The post object to which the comment belongs.
Examples
#1 Add a custom class to comments
Add the my-comment CSS class to all comments.
add_filter( 'comment_class', function( $classes ) {
$classes[] = 'my-comment';
return $classes;
} );
#2 Add a class to comments by the post author
Add the by-post-author class if the comment author is also the post author.
add_filter( 'comment_class', function( $classes, $_, $__, $comment, $post ) {
if ( $comment->user_id === $post->post_author ) {
$classes[] = 'by-post-author';
}
return $classes;
}, 10, 5 );Changelog
| Since 2.7.0 | Introduced. |
Where the hook is called
comment_class
wp-includes/comment-template.php 599
return apply_filters( 'comment_class', $classes, $css_class, $comment->comment_ID, $comment, $post );