The documentation on customizing WordPress comments is a bit sparse, particularly when compared to WordPress posts. I ran into this issue while building a web application built off of WordPress and wanted to use WordPress’ commenting system with some custom fields and styling. So here’s my attempt to address that.
- Make sure you have a comments.php template file for your theme – create one if needed. I recommend looking at _s for a good model.
- See the following code below – it should be pretty obvious with its inline commenting. Please excuse any WordPress code formatting issues.
- With this code we can now wrap your comment markup with our own tags and classes as well as display our own custom comment fields/data using get_comment_meta().
- The WordPress core comment-template.php file had the clues I needed to figure this out. I saw how they were structuring and coding things so I translated that code into my comments.php theme file, tested, and tidied it up until it worked. Now to the code…
/*
If the current post is protected by a password and
the visitor has not yet entered the password we will
return early without loading the comments.
*/
if ( post_password_required() ) {
return;
}
// Now our comments area
<div id="comments" class="comments-area">
//check to see if we have comments
if ( have_comments() ) :
<h2 class="comments-title">
printf( // WPCS: XSS OK.
esc_html( _nx( 'One comment on “%2$s”', '%1$s comments on “%2$s”', get_comments_number(), 'comments title', '_s' ) ),
number_format_i18n( get_comments_number() ),
'<span>' . get_the_title() . '</span>'
);
</h2>
if ( get_comment_pages_count() > 1 && get_option( 'page_comments' ) ) : // Are there comments to navigate through?
<nav id="comment-nav-above" class="navigation comment-navigation" role="navigation">
<h2 class="screen-reader-text"> esc_html_e( 'Comment navigation', '_s' ); </h2>
<div class="nav-links">
//comment navigation links
<div class="nav-previous"> previous_comments_link( esc_html__( 'Older Comments', '_s' ) ); </div>
<div class="nav-next"> next_comments_link( esc_html__( 'Newer Comments', '_s' ) ); </div>
</div><!-- .nav-links -->
</nav><!-- #comment-nav-above -->
endif; // Check for comment navigation.
<ol class="comment-list">
if ( have_comments() ) : //possibly duplicative, but this works
while (have_comments() ) : the_comment(); //the loop, but for comments
<li id="comment- comment_ID(); " comment_class(); >
<article id="div-comment- comment_ID(); " class="comment-body">
<footer class="comment-meta">
<div class="comment-author vcard">
echo get_avatar( $comment->comment_ID );
printf( __( '%s <span class="says">says:</span>' ), sprintf( '<strong class="fn">%s</strong>', get_comment_author_link() ) );
</div><!-- .comment-author -->
<div class="comment-metadata">
<a href=" echo esc_url( get_comment_link( $comment->comment_ID ) ); ">
<time datetime=" comment_time( 'c' ); ">
printf( _x( '%1$s at %2$s', '1: date, 2: time' ), get_comment_date(), get_comment_time() );
</time>
</a>
edit_comment_link( __( 'Edit' ), '<span class="edit-link">', '</span>' );
</div><!-- .comment-metadata -->
if ( '0' == $comment->comment_approved ) : //is the comment in moderation
<p class="comment-awaiting-moderation"> _e( 'Your comment is awaiting moderation.' ); </p>
endif;
</footer><!-- .comment-meta -->
<div class="comment-content">
comment_text(); //the actual comment content
</div><!-- .comment-content -->
comment_reply_link(); // the reply to a comment link
</article><!-- .comment-body -->
</li>
endwhile;
endif; //closing out our comment loop
</ol><!-- .comment-list -->
// If comments are closed and there are comments, let's leave a little note, shall we?
if ( ! comments_open() && get_comments_number() && post_type_supports( get_post_type(), 'comments' ) ) :
<p class="no-comments"> esc_html_e( 'Comments are closed.', '_s' ); </p>
endif;
comment_form(); //the comment form
</div><!-- #comments -->