Ultimate WordPress comments guide

There are many things you can do with WordPress comments. You can use for example default themes with default settings, Use linear or nested/threaded commenting. You can use social implementation, email verification, spam protection, show your comment form in a popup or add many extra features to optimise it exactly the way you want. You also have many ways to call the functions and show your comments and comment box.

In this guide I will show you all my tips and tricks that I have found during developing my WordPress projects and make one big guide with as much as possible information about WordPress commenting.

Play with WordPress comments

In WordPress comments you can choose for 2 options.

  1. Use default theme comments with save admin control and
  2. Play with your template and functions, to make it your own!

If you chose for the second I will show you how you can do it. (if you have better options? Please comment down)

First you need to make a file named comments.php. And call it with:

<?php comments_template(); ?>

This piece of code says to WordPress: “Take comments.php and show it”.

Then you need a comment template for example:

 * @package WordPress
 * @subpackage Default_Theme

// Do not delete these lines
	if (!empty($_SERVER['SCRIPT_FILENAME']) && 'comments.php' == basename($_SERVER['SCRIPT_FILENAME']))
		die ('Please do not load this page directly. Thanks!');

	if ( post_password_required() ) { ?>
		<p>This post is password protected. Enter the password to view comments.</p>

<!-- You can start editing here. -->

<?php if ( have_comments() ) : ?>
	<h3 id="comments"><?php comments_number('No Responses', 'One Response', '% Responses' );?> to “<?php the_title(); ?>”</h3>

		<?php wp_list_comments('type=comment&callback=advanced_comment'); //this is the important part that ensures we call our custom comment layout defined above 
		<div><?php previous_comments_link() ?></div>
		<div><?php next_comments_link() ?></div>
 <?php else : // this is displayed if there are no comments so far ?>

	<?php if ( comments_open() ) : ?>
		<!-- If comments are open, but there are no comments. -->

	 <?php else : // comments are closed ?>
		<!-- If comments are closed. -->
		<p class="nocomments">Comments are closed.</p>

	<?php endif; ?>
<?php endif; ?>

<?php if ( comments_open() ) : ?>

<div id="respond">

<h3><?php comment_form_title( 'Leave a Reply', 'Leave a Reply to %s' ); ?></h3>

	<small><?php cancel_comment_reply_link(); ?></small>

<?php if ( get_option('comment_registration') && !is_user_logged_in() ) : ?>
<p>You must be <a href="<?php echo wp_login_url( get_permalink() ); ?>">logged in</a> to post a comment.</p>
<?php else : ?>

<form action="<?php echo get_option('siteurl'); ?>/wp-comments-post.php" method="post" id="commentform">

<?php if ( is_user_logged_in() ) : ?>

<p>Logged in as <a href="<?php echo get_option('siteurl'); ?>/wp-admin/profile.php"><?php echo $user_identity; ?></a>. <a href="<?php echo wp_logout_url(get_permalink()); ?>" title="Log out of this account">Log out &raquo;</a></p>

<?php else : //this is where we setup the comment input forums ?>

<p><input type="text" name="author" id="author" value="<?php echo esc_attr($comment_author); ?>" size="22" tabindex="1" <?php if ($req) echo "aria-required='true'"; ?> />
<label for="author"><small>Name <?php if ($req) echo "(required)"; ?></small></label></p>

<p><input type="text" name="email" id="email" value="<?php echo esc_attr($comment_author_email); ?>" size="22" tabindex="2" <?php if ($req) echo "aria-required='true'"; ?> />
<label for="email"><small>Mail (will not be published) <?php if ($req) echo "(required)"; ?></small></label></p>

<p><input type="text" name="url" id="url" value="<?php echo esc_attr($comment_author_url); ?>" size="22" tabindex="3" />
<label for="url"><small>Website</small></label></p>

<?php endif; ?>

<!--<p><small><strong>XHTML:</strong> You can use these tags: <code><?php echo allowed_tags(); ?></code></small></p>-->

<p><textarea name="comment" id="comment" cols="100%" rows="10" tabindex="4"></textarea></p>

<p><input name="submit" type="submit" id="submit" tabindex="5" value="Post" />
<?php comment_id_fields(); ?>
<?php do_action('comment_form', $post->ID); ?>


<?php endif; // If registration required and not logged in ?>

<?php endif; // if you delete this the sky will fall on your head ?>

In this template are sevaral options, functions and if statements to make it a save and dynamic template. You can expand or minimalise it but I gave this as an example/base template.

If you look closely you see the comments being shown by

<?php wp_list_comments('type=comment&callback=advanced_comment');

In this function you see a “callback” function named “advanced_comment”. With this function we can say in functions.php how to show the comments.

Extra: There are more options for the <?php wp_list_comments(); ?> function:

<?php $args = array(
	'walker'            => null,
	'max_depth'         => '',
	'style'             => 'ul',
	'callback'          => null,
	'end-callback'      => null,
	'type'              => 'all',
	'reply_text'        => 'Reply',
	'page'              => '',
	'per_page'          => '',
	'avatar_size'       => 32,
	'reverse_top_level' => null,
	'reverse_children'  => ''
); ?>

So we make a function in functions.php called: “advanced_comment” and use for example a template like this:

function advanced_comment($comment, $args, $depth) {
   $GLOBALS['comment'] = $comment; ?>

<li <?php comment_class(); ?> id="comment-<?php comment_ID() ?>">
     <?php echo get_avatar($comment,$size='48',$default='<path_to_url>' ); ?>
       <div<a href="<?php the_author_meta( 'user_url'); ?>"><?php printf(__('%s'), get_comment_author_link()) ?></a></div>
       <small><?php printf(__('%1$s at %2$s'), get_comment_date(),  get_comment_time()) ?><?php edit_comment_link(__('(Edit)'),'  ','') ?></small>

     <?php if ($comment->comment_approved == '0') : ?>
       <em><?php _e('Your comment is awaiting moderation.') ?></em>
       <br />
     <?php endif; ?>

         <?php comment_text() ?>

      <?php comment_reply_link(array_merge( $args, array('depth' => $depth, 'max_depth' => $args['max_depth']))) ?>
<?php } ?>

In this template you see:

  • comment_class(); This helps WordPress giving classes to the comment like: comment, even (or odd), thread-even, depth-1, etc.. so you can easy style them different ways.
  • get_avatar This shows an avatar picture of the author.
  • get_comment_date Gives the date when the comment is placed.
  • edit_comment_link This shows a link where the admin user can edit the comment directly.
  • if($comment->comment_approved == ‘0’) : This checks of the comment is approved and if not it gived the waiting for moderation text.
  • comment_text This shows the content of the comment.
  • comment_reply_link Shows a link for replying.

Important WordPress comment template tags

Template Tag Description
<?php comment_ID(); ?> The ID of a comment
<?php comment_author(); ?> The author of a comment
<?php comment_author_link(); ?> The author of a comment, wrapped with a link to his website if he specified one
<?php comment_type(); ?> The type of comment; pingback, trackback or a comment
<?php comment_text(); ?> The actual comment
<?php comment_date(); ?> The date it was posted
<?php comment_time(); ?> The time it was posted

Decide whether a comment is parent or child

If you want decide whether a comment is parent or child (for example if you want to style it another way) then you can check it by the following code:

WordPress comments custom post type

If you want to use comments in custom post types you first need to add “comments” to your custom post type.

Comment e-mail notification

If you want your visitors notificated after submitting a comment  you can use the following plugins:

Comment e-mail moderation

If you want to do other thing with your comment moderation e-mails you can use the following plugins:

Other Helpful comment plugins

Simple Questions and Answers

How to turn on nested/threaded comments in WordPress admin?

To turn on nested/threaded comment in your WordPress admin you go to settings->Discussion->Other comment settings->Enable threaded (nested) comments X levels deep.



12 thoughts on “Ultimate WordPress comments guide

    • Thanks Ioannis, I will continue to complement this article with more comment snippets, when I find them. I hope I may help you with it!

  1. Attractive section of content. I just stumbled upon your website and in accession capital to assert that I get in fact enjoyed account your blog posts. Any way Ill be subscribing to your feeds and even I achievement you access consistently rapidly. ceebbdebde

  2. At first sight and tests I noticed in line 8 of the code for the function “advanced_comment” for functions.php a ‘>’ is missed to close the opening div-tag.
    I could imagine it should be ‘… <a href="<?php the_author_meta …'.
    Thanks for this guide anyway.

  3. I’ve tried it so far but unfortunately it doesn’t work and give me error page , don’t know the reason
    can you upload a php complete file code for me , thanks in advance

  4. Thanks for the article. As well is the most comprehensive article I found. I have just one problem: I’m using _S (Underscore theme) and this custom comment and unfortunately the ajax / jquery doesn’t work for reply button..

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s