How to Add Commenting to WordPress Page Template

If you want to enable commenting on a WordPress page (as opposed to a post), you would need to add the following in the theme’s page.php file, which is the default template for pages:

<?php comments_template(); ?>

If you created a custom template file, you would need to put the code in that file also.

That’s pretty straightforward since that is the same code used in single.php to add commenting.

However, it’s not enough. If you try to submit a comment, it will go to a blank page with this in the address bar:

http://domain.com/blog/wp-comments-post.php

I compared the outputted html from a post and a page. The difference turned out to be

<input type="hidden" name="comment_post_ID" value="" />

where the value was supposed to have the post id. Because of the incomplete data sent, WordPress spit out a blank page.

A WordPress “page” does get assigned a post id, just like a post does. Somewhere, the comment_post_ID value was not being entered for pages.

Looking in the theme’s comments.php file, this was the relevant code:

<input type="hidden" name="comment_post_ID" value="<?php echo $id; ?>" />

I could not find where $id was defined. So I added this line:

<?php $id = $post->ID; ?>

I was then able to successfully add a comment to a page.

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>