The leanest comments.php file ever

More to be seen as a proof of concept than the next step in Theme Development evolution, in version 1.7.0 The Bootstrap’s comments.php file will contain not more than a single call to comments_form().

[php]
<?php
/** comments.php
*
* The template for displaying Comments.
*
* The area of the page that contains both current comments
* and the comment form. The actual display of comments is
* handled by callbacks which are located in the functions.php file.
*
* @author Konstantin Obenland
* @package The Bootstrap
* @since 1.0.0 – 05.02.2012
*/

comment_form();

/* End of file comments.php */
/* Location: ./wp-content/themes/the-bootstrap/comments.php */
[/php]

Many Theme authors will argue that it doesn’t make much sense to move the entire comment business out of comments.php and they are probably right. But when I realized that it was possible to have the entire file consist of only one function call, I wanted to implement it and try it out.

This is what I moved to functions.php:

[php]
/**
* Displays comment list, when there are any
*
* @author Konstantin Obenland
* @since 1.7.0 – 16.06.2012
*
* @return void
*/
function the_bootstrap_comments_list() {
if ( post_password_required() ) : ?>
<div id="comments">
<p class="nopassword"><?php _e( ‘This post is password protected. Enter the password to view any comments.’, ‘the-bootstrap’ ); ?></p>
</div><!– #comments –>
<?php
return;
endif;

if ( have_comments() ) : ?>
<div id="comments">
<h2 id="comments-title">
<?php printf( _n( ‘One thought on &ldquo;%2$s&rdquo;’, ‘%1$s thoughts on &ldquo;%2$s&rdquo;’, get_comments_number(), ‘the-bootstrap’ ),
number_format_i18n( get_comments_number() ), ‘<span>’ . get_the_title() . ‘</span>’ ); ?>
</h2>

<?php the_bootstrap_comment_nav(); ?>

<ol class="commentlist unstyled">
<?php wp_list_comments( array( ‘callback’ => ‘the_bootstrap_comment’ ) ); ?>
</ol><!– .commentlist .unstyled –>

<?php the_bootstrap_comment_nav(); ?>

</div><!– #comments –>
<?php endif;
}
add_action( ‘comment_form_before’, ‘the_bootstrap_comments_list’, 0 );
add_action( ‘comment_form_comments_closed’, ‘the_bootstrap_comments_list’, 1 );

/**
* Echoes comments-are-closed message when post type supports comments and we’re
* not on a page
*
* @author Konstantin Obenland
* @since 1.7.0 – 16.06.2012
*
* @return void
*/
function the_bootstrap_comments_closed() {
if ( ! is_page() AND post_type_supports( get_post_type(), ‘comments’ ) ) : ?>
<p class="nocomments"><?php _e( ‘Comments are closed.’, ‘the-bootstrap’ ); ?></p>
<?php endif;
}
add_action( ‘comment_form_comments_closed’, ‘the_bootstrap_comments_closed’ );
[/php]

So there you have it! What do you think?

10 thoughts on “The leanest comments.php file ever

  1. Hi Konstantin

    I wonder, would it be possible to override the functions that’s been moved to functions.php, somehow? I ask this, because I’m building a hopefully better comment system, and I replace comments.php with a modified version. (I like Twitter Bootstrap and that’s how I found your blog.)

    Best regards, KajMagnus

  2. I wonder, would it be possible to override the functions that’s been moved to functions.php, somehow? I ask this, because I’m building a hopefully better comment system, and I replace comments.php with a modified version. (I like Twitter Bootstrap and that’s how I found your blog.)

Leave a Reply

Your email address will not be published. Required fields are marked *