Print Comments anywhere, and the Form as well

Drupals standard practice of printing node comments below the node can get a bit stale. Sometimes you may want, or simply need to, print them somewhere else. Wouldn't be great if comments and the comment form were just variables you could move around?

Well you can—thanks to Drupal 6's phptemplate_preprocess_page function we can easily store both the comments and the comment form in variables. The only caveat is that *you must set the comment reply form to a separate page first*, or something might break.

The following snippets store the comment form and comments in variables and allow you print them in page.tpl.php.

// Store comments and the comment form in variables
function phptemplate_preprocess_page(&$vars) {
$vars['comments'] = $vars['comment_form'] = '';
  if (
module_exists('comment') && isset($vars['node'])) {
$vars['comments'] = comment_render($vars['node']);
$vars['comment_form'] = drupal_get_form('comment_form',
'nid' => $vars['node']->nid));
// Unset comments from node
function phptemplate_preprocess_node(&$vars) {
$vars['node']->comment = 0;

Now you can print $comments and $comment_form anywhere in page.tpl.php, and they will only print when you are viewing a node.

Last updated 15th April, 2009 - 11:51am

Authored by Jeff Burnz on