wppaste
WordPress

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
Returns the classes for the comment div as an array.

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

Reaches the database via get_post().

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
13–113

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 120.

Plugin surface
1 hook

Third-party callbacks on 'comment_class' run inside this call, and their cost is not bounded by anything here.

Called by
2

2 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • querycontent queryget_post()called directly
  • hookthird-party callbacksapply_filters()called directly
  • regexregular expression over the whole inputpreg_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.

WhenInstructionsCalls it makes
always13get_comment()
!$comment && empty($css_class)56–71get_comment(), array_map(), apply_filters()
$comment && empty($css_class)62–77get_comment(), get_userdata(), array_map(), apply_filters()
!$comment && !empty($css_class) && is_array($css_class)63–78get_comment(), array_merge(), array_map(), apply_filters()
!$comment && !empty($css_class) && !is_array($css_class)68–83get_comment(), preg_split(), array_merge(), array_map(), apply_filters()
$comment && !empty($css_class) && is_array($css_class)69–84get_comment(), get_userdata(), array_merge(), array_map(), apply_filters()
!$comment && empty($css_class)74–95get_comment(), sanitize_html_class(), get_post(), array_map(), apply_filters()
$comment && !empty($css_class) && !is_array($css_class)74–89get_comment(), get_userdata(), preg_split(), array_merge(), array_map(), apply_filters()
$comment && empty($css_class)80–101get_comment(), get_userdata(), sanitize_html_class(), get_post(), array_map(), apply_filters()
!$comment && !empty($css_class) && is_array($css_class)81–102get_comment(), sanitize_html_class(), get_post(), array_merge(), array_map(), apply_filters()
!$comment && !empty($css_class) && !is_array($css_class)86–107get_comment(), sanitize_html_class(), get_post(), preg_split(), array_merge(), array_map(), apply_filters()
$comment && !empty($css_class) && is_array($css_class)87–108get_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–113get_comment(), get_userdata(), sanitize_html_class(), get_post(), preg_split(), array_merge(), array_map(), apply_filters()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev11913–112141 fewer instruction than PHP 8.5
8.512013–11314
8.412013–11314
8.312013–11314
8.212013–11314
8.112013–113141 fewer instruction than PHP 7.4
7.412113–11314

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:

  1. apply_filters( comment_class )filterline 599 (+79 into the body)

    Filters the returned CSS classes for the current comment.

Uses · 5

Used by · 2

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.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

4.4.0
Added the ability for $comment_id to also accept a WP_Comment object.from the docblock
2.7.0
Introduced.from the docblock

About 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.