4.6.x core.php hook_comment($op, $comment)
4.7.x core.php hook_comment($a1, $op)
5.x core.php hook_comment(&$a1, $op)
6.x core.php hook_comment(&$a1, $op)

Respond to comment actions.

This hook allows modules to extend the comments system by responding when certain actions take place.


$a1: Argument; meaning is dependent on the action being performed.

  • For "validate", "update", and "insert": an array of form values submitted by the user.
  • For all other operations, the comment the action is being performed on.

$op: The action being performed. Possible values:

  • "insert": The comment is being inserted.
  • "update": The comment is being updated.
  • "view": The comment is being viewed. This hook can be used to add additional data to the comment before theming.
  • "validate": The user has just finished editing the comment and is trying to preview or submit it. This hook can be used to check the comment. Errors should be set with form_set_error().
  • "publish": The comment is being published by the moderator.
  • "unpublish": The comment is being unpublished by the moderator.
  • "delete": The comment is being deleted by the moderator.

Related topics

7 functions implement hook_comment()

Note: this list is generated by pattern matching, so it may include some functions that are not actually implementations of this hook.

chameleon_comment in themes/chameleon/chameleon.theme
comment_invoke_comment in modules/comment/comment.module
Invoke a hook_comment() operation in all modules.
search_comment in modules/search/search.module
Implementation of hook_comment().
template_preprocess_comment in modules/comment/comment.module
Process variables for comment.tpl.php.
theme_comment in developer/theme.php
Implemented using the comment.tpl.php template.

... See full list


developer/hooks/core.php, line 270
These are the hooks that are invoked by the Drupal core.


function hook_comment(&$a1, $op) {
  if ($op == 'insert' || $op == 'update') {
    $nid = $a1['nid'];
    'id' => $nid,


__mark’s picture

the 'unpublish' $op doesn't actually exist. Which makes total sense, since the 'publish' $op does exist... wait... what?

A possible workaround is to use 'update', and check the value of the comment's status, to determine if has just been unpublished.

__mark’s picture

If you're publishing/unpublishing from the comment/edit/* page, the $a1 in hook_comment() is going to be an array, but if you're doing that from the admin/content/comment/approval page, $a1 is actually the comment object.

Hope this saves someone else a few hours.

charlie-s’s picture

Apparently this doc page isn't even remotely accurate.


zombree’s picture

Seriously, this caused me some frustration. Read the discussion at the link above. I finally got my implementation of hook_comment() working by using the parameter &$comment instead of &$a1 like they did in user_comment().

rogical’s picture

Any substitutions for D7?

subu.purohit’s picture


I wand to do some changes in comment body before comment save (need to replace some text in comment). I am trying to add this:

switch ($op) {
case 'insert':
$comment['comment'] = changeText($comment['comment']);
But changes are not reflecting after comment save. However 'view' is working properly. I am working on drupal 6.

Any idea?

atoledo@ciandt.com’s picture

The changes were not saved because the $op 'insert' is called after the comment is saved on the database. What you can do in order to do what you want is call comment_save() with the updated text inside your hook implementation