Changing How WordPress Displays Comments

C

Comments in WordPress are displayed using the wp_list_comments() function. This function takes many arguments. One of these is callback which takes the name of a defined function that in turn determines what and how comment data is output. If no callback function is given, wp_list_comments() uses a built-in default function.

I recently wanted to make a very minor change to how comments are displayed on this site. As such, I didn’t want to have to write a callback function from scratch and thought it easier to use the built-in default function as a template. But where is that function? It took me a while to track it – them – down, so for any one in a similar situation the two functions are comment() and html5_comment(). They are both defined in the file wp‑includes/class‑walker‑comment.php.

Which function is used depends on the value of the format option passed to wp_list_comments(). The comment() function is used if the value is xhtml or the html5_comment() function if the value is html5. The value defaults to the current theme’s current_theme_supports( ‘html5’ ) setting.

Below is an example of a callback function named format_comment(). The code starting on line 6 and ending on line 50 is copied directly from the html5_comment() function and has not been altered.

function format_comment($comment, $args, $depth) {

	$GLOBALS['comment'] = $comment; ?>
   
	<li id="li-comment-<?php comment_ID() ?>" <?php comment_class(); ?>>
		<article id="div-comment-<?php comment_ID(); ?>" class="comment-body">
			<footer class="comment-meta">
				<div class="comment-author vcard">
					<?php if ( 0 != $args['avatar_size'] ) echo get_avatar( $comment, $args['avatar_size'] ); ?>
					<?php
						/* translators: %s: comment author link */
						printf( __( '%s <span class="says">says:</span>' ),
							sprintf( '<b class="fn">%s</b>', get_comment_author_link( $comment ) )
						);
					?>
				</div><!-- .comment-author -->

				<div class="comment-metadata">
					<a href="<?php echo esc_url( get_comment_link( $comment, $args ) ); ?>">
						<time datetime="<?php comment_time( 'c' ); ?>">
							<?php
								/* translators: 1: comment date, 2: comment time */
								printf( __( '%1$s at %2$s' ), get_comment_date( '', $comment ), get_comment_time() );
							?>
						</time>
					</a>
					<?php edit_comment_link( __( 'Edit' ), '<span class="edit-link">', '</span>' ); ?>
				</div><!-- .comment-metadata -->

				<?php if ( '0' == $comment->comment_approved ) : ?>
				<p class="comment-awaiting-moderation"><?php _e( 'Your comment is awaiting moderation.' ); ?></p>
				<?php endif; ?>
			</footer><!-- .comment-meta -->

			<div class="comment-content">
				<?php comment_text(); ?>
			</div><!-- .comment-content -->

			<?php
			if (get_comment_type() == 'comment') {
				comment_reply_link( array_merge( $args, array(
					'add_below' => 'div-comment',
					'depth'     => $depth,
					'max_depth' => $args['max_depth'],
					'before'    => '<div class="reply">',
					'after'     => '</div>'
				) ) );
			}
			?>
		</article><!-- .comment-body -->
        
<?php }	// function format_comment()

 

 

Below are two examples of using wp_list_comments(). The first has no callback function, just the type of comment to include. No format option is given either, but we can assume it will default to html5. As such, comments are displayed using the default html5_comment() function.

<?php
	$args = array( 'type' => 'comment' );	// No 'callback' function is given. Assume 'format' defaults to 'html5' so the default html5_comment() function is used. 
	wp_list_comments( $args ); 
?>

 

 

In the second example, the callback function format_comment() is passed to wp_list_comments() which is then used to display comments instead of the default html5_comment().

<?php
	$args = array( 'type' => 'comment', 'callback' => 'format_comment' );	// As a 'callback' function is given, format_comment() is used instead of the default html5_comment() function.
	wp_list_comments( $args ); 
?>

 

 

As format_comment() and html5_comment() are virtually the same at this point, both of the above examples output comment data in an identical fashion. However, we can now ammend format_comment() to change the output of comment data without having to write a callback function from scratch.

About the author

A native Brit exiled in Japan, Steve spends too much of his time struggling with the Japanese language, dreaming of Fish & Chips and writing the occasional blog post he hopes others will find helpful.

Add comment

Steve

Recent Comments

Recent Posts

Archives