WordPress comments on home page

I recently built a modified version of WordPress’s Twenty Eleven theme for a graphic designer on behalf of their customer.

One of the things that the customer wanted was to put the “comments” form onto the bottom of each post on the home page of the blog.

That turned out to be pretty easy to do. In the index.php file inside the template, just add the following line of code under the part that outputs the post content:

 <?php comments_template( '', true ); ?>

The problem, as we discovered, is that when users replied to comments, the new comments were all appearing under the first post on the page.

My initial guess – which turned out to be wrong – was that there was some kind of bug in WordPress which had recently been introduced.

I dug a little deeper and looked at the stock comment-template.php file that comes with WordPress. When it builds the “reply-to” links, it creates a relative link based on the current page (i.e. normally that would be the blog post page itself), and then also adds in some javascript which keeps the user on the page and defaults the comment form correctly.

The problem was that the javascript necessary wasn’t included into the current page.

The fix consisted of adding the following line into the top of the index.php file:

wp_enqueue_script( 'comment-reply' );

That still leaves a side issue – what happens if a user has javascript disabled?

The correct fix would be to include a comments-template file into the theme, including a fix that includes the post id (when necessary) in the link url. The side issue with that (not really a big deal, but anyhow) is that you would then need to modify any other code in the theme that calls the comments template, to ensure it is calling the new one in the theme instead.

i.e. search for:

<?php comments_template( '', true ); ?>

and replace with:

<?php comments_template( '/newcomments-template.php'', true ); ?>

Not a big deal, but annoying.

Ideally though, this is something that WordPress should fix in their default code and/or something that theme developers should include by default.