How can I programmatically enable permission for anonymous users to view comments on a single node? Comments are open, but by default anonymous users are not allowed to view comments. I would like them to on one single node.


If all you are wanting to do is allow anonymous users to view comments on a single node, you could take the dull ax approach by first allowing all anonymous users to view comments, eg:

enter image description here

and then, in your theme, copying the default node.tpl.php to node--###.tpl.php where ### is the nid of the node you are wanting your anonymous users to View the comments and then making the following changes:

In node.tpl.php, there should be something along these lines near the end of the file:

<?php print render($content['comments']); ?>

This is where the actual comments are displayed. Since this is the template that will be used for every node except the one you want special comment treatment, change this to:

<?php if ($logged_in): ?>
  <?php print render($content['comments']); ?>
<?php endif; ?>

which will have the effect of displaying the comments only if the user is logged in. And, since the node--###.tpl.php doesn't have this newly added if surrounding its printing of the comments, it will display the comments for both anonymous and authenticated/logged in users.

Of course, you could also just have modified node.tpl.php like this:

<?php if ($logged_in || $node->nid==###): ?>
  <?php print render($content['comments']); ?>
<?php endif; ?>

and not copied it to node--###.tpl.php, but then if you wanted to extend this to another node, you'd have to enter the logic directly in the template, and not just copying node--###.tpl.php to node--######.tpl.php.

Just remember to flush your caches to let Drupal know about your changes.


There are a couple ways to handle this and it depends on when you want to make the change.

What you'll want to change is $node->comment to one of the following:

You could use a hook_node_presave() function to change the value prior to saving based on some logic. Or you just need to get access to the node object through other means like node_load(). Just make sure to save the node if you're loading it up to make the change.

  • Thanks, comments are already open, but anon users are denied access. This is how things are set in my permissions. I want to overide the access permission from FALSE to TRUE for a single node. hook_menu_alter??? – dbj44 Feb 2 '14 at 19:18
  • Ah, hmm. I don't think there is a way to give someone permission to enter comments per node. Depending on how you render your pages, what you could do is enable anonymous commenting (permissions) but hide/remove the comment form for all nodes except the nodes you want anonymous users to comment on. It's somewhat backwards but I think that's a limitation on the comments right now. Take a look at the drupal.org/project/comment_allow_anonymous module as an example. It deals with comments per node, but it should give you an idea. – Craig Feb 2 '14 at 21:36
  • That's a tiny module when you look inside :) Didn't really help. Maybe I'll take a look inside the comment module and serach for user_access and see when and where that function gets called. Thanks for trying. – dbj44 Feb 2 '14 at 22:11
  • What that module demonstrates is a) enabling anonymous commenting permissions, b) what form alters to mess with to show/hide the comment form, c) adding a callback to a custom function to determine whether the anonymous user can post comments. In that module's case it was based on the content type. So what you would have to do is figure out how you're going to track what nodes the anonymous user can comment on. If it's just a few then you can use variables, otherwise you might want a new schema. Your custom function would check the node (instead of node type). – Craig Feb 2 '14 at 22:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.