get_comment_class( string|string[] $css_class = '', int|WP_Comment $comment_id = null, int|WP_Post $post = null ): string[]
- Since
- 2.7.0, 4.4.0
- Source
wp-includes/comment-template.php:520
Compatibility
- WordPress
- since 4.4.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Parameters
$css_classstring|string[]optional- One or more classes to add to the class list.
Default empty.Default:'' $comment_idint|WP_Commentoptional- Comment ID or WP_Comment object. Default current comment.Default:
null $postint|WP_Postoptional- Post ID or WP_Post object. Default current post.Default:
null
Return value
string[]- An array of classes.
Performance profile
How much work a call to get_comment_class() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Heavy
- Scaling
- Constant
- Instructions
- 13–113
- Plugin surface
- 1 hook
- Called by
- 2
Reaches the database via get_post().
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 120.
Third-party callbacks on 'comment_class' run inside this call, and their cost is not bounded by anything here.
2 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- querycontent query
get_post()called directly - hookthird-party callbacks
apply_filters()called directly - regexregular expression over the whole input
preg_split()called directly
Further down the call graph this can also reach option, cache, serialize and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 13 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost get_comment_class() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 13 | get_comment() |
!$comment && empty($css_class) | 56–71 | get_comment(), array_map(), apply_filters() |
$comment && empty($css_class) | 62–77 | get_comment(), get_userdata(), array_map(), apply_filters() |
!$comment && !empty($css_class) && is_array($css_class) | 63–78 | get_comment(), array_merge(), array_map(), apply_filters() |
!$comment && !empty($css_class) && !is_array($css_class) | 68–83 | get_comment(), preg_split(), array_merge(), array_map(), apply_filters() |
$comment && !empty($css_class) && is_array($css_class) | 69–84 | get_comment(), get_userdata(), array_merge(), array_map(), apply_filters() |
!$comment && empty($css_class) | 74–95 | get_comment(), sanitize_html_class(), get_post(), array_map(), apply_filters() |
$comment && !empty($css_class) && !is_array($css_class) | 74–89 | get_comment(), get_userdata(), preg_split(), array_merge(), array_map(), apply_filters() |
$comment && empty($css_class) | 80–101 | get_comment(), get_userdata(), sanitize_html_class(), get_post(), array_map(), apply_filters() |
!$comment && !empty($css_class) && is_array($css_class) | 81–102 | get_comment(), sanitize_html_class(), get_post(), array_merge(), array_map(), apply_filters() |
!$comment && !empty($css_class) && !is_array($css_class) | 86–107 | get_comment(), sanitize_html_class(), get_post(), preg_split(), array_merge(), array_map(), apply_filters() |
$comment && !empty($css_class) && is_array($css_class) | 87–108 | get_comment(), get_userdata(), sanitize_html_class(), get_post(), array_merge(), array_map(), apply_filters() |
1 further outcome, up to 113 instructions
$comment && !empty($css_class) && !is_array($css_class) | 92–113 | get_comment(), get_userdata(), sanitize_html_class(), get_post(), preg_split(), array_merge(), array_map(), apply_filters() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 119 | 13–112 | 14 | 1 fewer instruction than PHP 8.5 |
| 8.5 | 120 | 13–113 | 14 | |
| 8.4 | 120 | 13–113 | 14 | |
| 8.3 | 120 | 13–113 | 14 | |
| 8.2 | 120 | 13–113 | 14 | |
| 8.1 | 120 | 13–113 | 14 | 1 fewer instruction than PHP 7.4 |
| 7.4 | 121 | 13–113 | 14 |
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Hooks and filters fired · 1
One hook fires while get_comment_class() runs, in this order:
- apply_filters( comment_class )filterline 599 (+79 into the body)
Filters the returned CSS classes for the current comment.
Uses · 5
- get_comment()Retrieves comment data given a comment ID or comment object.
- get_userdata()Retrieves user info by user ID.
- sanitize_html_class()Sanitizes an HTML classname to ensure it only contains valid characters.
- get_post()Retrieves post data given a post ID or post object.
- apply_filters()Calls the callback functions that have been added to a filter hook.
Used by · 2
- WP_Comments_List_Table::single_row()
- comment_class()Generates semantic classes for each comment element.
Source code
function get_comment_class( $css_class = '', $comment_id = null, $post = null ) { global $comment_alt, $comment_depth, $comment_thread_alt; $classes = array(); $comment = get_comment( $comment_id ); if ( ! $comment ) { return $classes; } // Get the comment type (comment, trackback). $classes[] = ( empty( $comment->comment_type ) ) ? 'comment' : $comment->comment_type; // Add classes for comment authors that are registered users. $user = $comment->user_id ? get_userdata( $comment->user_id ) : false; if ( $user ) { $classes[] = 'byuser'; $classes[] = 'comment-author-' . sanitize_html_class( $user->user_nicename, $comment->user_id ); // For comment authors who are the author of the post. $_post = get_post( $post ); if ( $_post ) { if ( $comment->user_id === $_post->post_author ) { $classes[] = 'bypostauthor'; } } } if ( empty( $comment_alt ) ) { $comment_alt = 0; } if ( empty( $comment_depth ) ) { $comment_depth = 1; } if ( empty( $comment_thread_alt ) ) { $comment_thread_alt = 0; } if ( $comment_alt % 2 ) { $classes[] = 'odd'; $classes[] = 'alt'; } else { $classes[] = 'even'; } ++$comment_alt; // Alt for top-level comments. if ( 1 === $comment_depth ) { if ( $comment_thread_alt % 2 ) { $classes[] = 'thread-odd'; $classes[] = 'thread-alt'; } else { $classes[] = 'thread-even'; } ++$comment_thread_alt; } $classes[] = "depth-$comment_depth"; if ( ! empty( $css_class ) ) { if ( ! is_array( $css_class ) ) { $css_class = preg_split( '#\s+#', $css_class ); } $classes = array_merge( $classes, $css_class ); } $classes = array_map( 'esc_attr', $classes ); /** * Filters the returned CSS classes for the current comment. * * @since 2.7.0 * * @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 apply_filters( 'comment_class', $classes, $css_class, $comment->comment_ID, $comment, $post );Changelog
Introduced in 2.7.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
$comment_id to also accept a WP_Comment object.from the docblockAbout this page
- Parsed data
- Generated from the wordpress-develop 6.8.8 tag, from
src/wp-includes/comment-template.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.