Byronyasgur's Blog

Random Web Developer Stuff, Mostly WordPress

WordPress Comments (comments.php file)

on September 9, 2011

I recently needed to do a comments.php file for a WordPress template and found it pretty hard to get to the bottom of some of the newer coding principles. The comments.php file is possibly the most involved piece of a theme. WordPress 2.7 had brought in threaded comments and WordPress 3.0 added the comment_form() function.

I looked at a few different themes, including one “starter theme” but the comments.php file in each didn’t suit me for different reasons in each case. Some of the themes were using old code in one place or another. The ones that weren’t were using callback and end callback functions with the wp_list_comments() function, which is fine if you need a lot of control over the layout but for me it wasn’t necessary as I’m primarily using WordPress as a CMS for my clients and the blogs they require are pretty standard, however I can easily extend the code in that way if I want in future.

What I was looking for was a straightforward simple file which didn’t use elaborate coding on wp_list_comments() or comment_form() but did use those two functions ( which themselves make the code more straightforward and in the case of comment_form() more friendly to plugin developers). It’s quite possible that one or more of the starter themes or blank themes has something similar to this but seeing as I’ve gone ahead and done it now I thought I might share it here. The code is based on what came with the latest Kubrick theme. It renders pretty good without any CSS styling but you’ll definitely want to add some eventually for a proper project. In the code below I’ve actually left ( commented out) array values for the arguments for the two functions which make the magic happen wp_list_comments() and comment_form(), and there are links to the relevant codex pages too if you want to play with these.

Finally note that I intentionally removed all the internationalization during development; so if you want to use this file as part of a theme which would be made available for use across more than a single language then as you probably know it would be important to re-internationalize the code. In my case I rarely ever develop anything which would use anything other than English so I removed the extra code for speed in development; as i will be using this file as a base template for other work. If anyone wants to send me a re-internationalized version I will include it here.

So without any more ado, the code.

 *  Comments themplate - Adapted Version of Kubrick's
 *  There are 2 distinct sections after the protection area ::
 *  Display Comments is a loop surrounding the wp_list_comments() function
 *  The Form/Login section uses comment_form() to do everything
 *  ( Finally the RSS link is at the end of the page )
 *  */

// ##########  Do not delete these lines
if (isset($_SERVER['SCRIPT_FILENAME']) && 'comments.php' == basename($_SERVER['SCRIPT_FILENAME'])){
	die ('Please do not load this page directly. Thanks!'); }
if ( post_password_required() ) { ?>
	<p class="nocomments"><?php _e('This post is password protected. Enter the password to view comments.', 'kubrick'); ?></p>
	return; }
// ##########  End do not delete section

// Display Comments Section
if ( have_comments() ) : ?>
  	<h3 id="comments"><?php comments_number('No Responses', 'One Response', '% Responses');?> <?php printf('to “%s”', the_title('', '', false)); ?></h3>
    	<div class="navigation">
    		<div class="alignleft"><?php previous_comments_link() ?></div>
    		<div class="alignright"><?php next_comments_link() ?></div>
  	<ol class="commentlist">
      // see
      // 'login_text'        => 'Login to reply',
      // 'callback'          => null,
      // 'end-callback'      => null,
      // 'type'              => 'all',
      // 'avatar_size'       => 32,
      // 'reverse_top_level' => null,
      // 'reverse_children'  =>
    	<div class="navigation">
    		<div class="alignleft"><?php previous_comments_link() ?></div>
    		<div class="alignright"><?php next_comments_link() ?></div>
    if ( ! comments_open() ) : // There are comments but comments are now closed
        echo"<p class='nocomments'>Comments are closed.</p>";

else : // I.E. There are no Comments
    if ( comments_open() ) : // Comments are open, but there are none yet
        // echo"<p>Be the first to write a comment.</p>";
    else : // comments are closed
        echo"<p class='nocomments'>Comments are closed.</p>";

// Display Form/Login info Section
// the comment_form() function handles this and can be used without any paramaters simply as "comment_form()"
  // see codex for default values
  // tutorial here
  'comment_field' => '<p><textarea name="comment" id="comment" cols="58" rows="10" tabindex="4" aria-required="true"></textarea></p>',
  'label_submit' => 'Submit Comment',
  'comment_notes_after' => ''

// RSS comments link
echo '<div class="comments_rss">';
comments_rss_link('Comments RSS Feed');
echo '</div>';


20 responses to “WordPress Comments (comments.php file)

  1. Hi! I’m trying to add the option of “Notify me of follow-up comments via email” to my theme but I don’t want to use a plug-in. I’d like to find the code that allows it to happen but I have failed. I’ve been searching the web for the past few days with no results.

    I know offers that function but I want it for a self-hosted blog.

    Do you know how to make it happen? Or can you give me a hand in which direction to go? All my searches end in nothing other than plug-ins 😦

    Thanks in advance! Martín.

  2. byronyasgur says:

    Sorry I’ve never looked into that code at all so I cant help, but I would definitely recommend asking the question on , which is a fantastic community, but if you’re not familiar with these sites then take a while to read the FAQ as it’s a Q&A site and very different to a forum or blog.
    I hope you have a reasonable reason for not wanting to use a plugin, like you want to sell the theme or something, because some people have funny ideas about plugins seeing them as add-ons which were not part of the original design, but this is not the case at all and plugins are a fantastic way of extending wordpress. I always have several plugins on a WP site, and provided you use well written plugins that you are fairly sure are going to be either regularly updated or else replaceable then you’re in good order.

  3. Thanks for your answer! I’ll check that out. The reason of not wanting to use a plug-in (I also use them on my sites but not on client sites) is that I’m developing a theme for a client and it doesn’t look very good if I ask him to install a plug-in because I couldn’t add this feature myself. (I think it doesn’t look good, I might be very wrong, off course!)

    And the other reason was to learn how to do it, so I’ll keep on trying. Again, thanks for your answer and congrats on your blog. I found it yesterday and I really liked it!


    • byronyasgur says:

      yes I was thinking it might be something like that – i myself do use them on client sites, but i would completely understand you not wanting to, although it’s worth considering that the client does have to pay the extra development hours, so in a sense isn;t it costing the client money ….. but anyway, another thought i had was just to find the simplest one of these plugins and sort of reverse engineer it a bit, or at least work out how they’re doing it – i don’t see anything wrong with that myself but I’m no expert in licensing or ethics or anything, so i’m open to correction.

  4. That’s a nice idea. I’m sure I’ll have to try something like that if I can’t find what I want. I tried finding the code on my client’s current theme but I didn’t succeeded.

    LAST MINUTE UPDATE! I placed a question on the site you forwarded me and as I’m writing this lines I got an answer! I’ll check it out now!


  5. Twiggit says:

    Hi There,

    I tried to use your comments.php file and I get a message that says:

    The comments plugin requires an href parameter

    Any ideas on what might be the problem?

    Thanks in advance

  6. byronyasgur says:

    What about plugins – you should disable them all and see if one of them is causing a conflict – it definitely seems to have something to do with facebook, in particular some facebook comments plugin like Facebook Comments for WordPress.

  7. says:

    Thankz…Its work…

  8. Jeff says:

    Thanks so much I’ve been trying to get nested comments working in my theme for 2 nights now. A little css and on to the next project thanks again!

  9. Jeff says:

    Just a little something to add: if you put
    if ( is_singular() ) wp_enqueue_script( ‘comment-reply’ ); dont forget to wrap with php tags
    n your header.php file on the line above
    wp_head(); dont forget to wrap with php tags
    that will make your comment box jump under the comment you are replying to 🙂

  10. mindi smith says:

    This website was… how do you say it? Relevant!
    ! Finally I have found something which helped me. Thanks!

  11. max says:

    Thanks for your code …

    Please use ” post_comments_feed_link() ” function for feed display , comments_rss_link(‘Comments RSS Feed’); Deprecated since version 2.5.

  12. Ravikrishna says:

    Thanks for giving code…

  13. Mark says:

    Just used this code for one of my blog.

    Thank you!

  14. Les says:

    Here’s a way to also get the WP editor to show in comments (works well with this comments.php code):

    You add this code to your themes functions.php file.

    Please also note Adam’s reply to Jeff above; making the editor jump under the comment you are replying to (if you have threaded comments enabled in WP-admin/settings/discussion).

  15. eman says:

    hi i need the code with reply like the comments in this page for my wordpress

Comments are closed.
%d bloggers like this: