Hacks : Avoiding Duplicate Comments 3.01 Apr 09 2003 | # | C:20

April 09, 2003 - updated October 06, 2004

When a visitor submits a comment, sometimes it takes a while for the comment page to refresh to reflect that their comment was successfully added. During this time, the user may click the “Post” button again (and again…) which causes their comment to be submitted multiple times.

This hack modifies MovableType to simply ignore the same comment being entered multiple times for an entry. Two comments are considered the same if the comment text and the name, email address and url of the comment auther are exactly the same.

Note for users of Jay Allen’s MT-Blacklist: Instead of modifying Comments.pm, you should modify extlib/jayallen/MTBlPost.pm (in older versions of MT-Blacklist modify extlib/jayallen/Blacklist.pm).

lib/MT/App/Comments.pm Modification

Go to your MovableType installation directory, and open the file Comments.pm in the lib/MT/App subdirectory. Find the line that says:

    $comment->save;

and insert the following code before it:

    ## BEGIN HACK Avoid Duplicate Comments
    ## http://www.nonplus.net/software/mt/AvoidingDuplicateComments.htm
    if(my @existing_comments = MT::Comment->load({
                  blog_id => $comment->blog_id,
                  entry_id => $comment->entry_id})) {

      foreach my $c (@existing_comments) {
          next unless (($c->author||'') eq ($comment->author||'')
                  && ($c->email||'') eq ($comment->email||'')
                  && ($c->text||'') eq ($comment->text||''));

          my $link_url;
          if (!$q->param('static')) {
              my $url = $app->base . $app->uri;
              $url .= '?entry_id=' . $q->param('entry_id');
              $link_url = $url;
          } else {
              my $static = $q->param('static');
              if ($static == 1) {
                  $link_url = $entry->permalink;
              } else {
                  $link_url = $static . '#' . $c->id;
              }
          }
          return $app->redirect($link_url);
      }
    }
    ## END HACK Avoid Duplicate Comments

In MovableType 2.51 and 2.63, this modification is on lines 99 and 106, respectively.

Version History
  • 3.01 October 14 2003
    Describe integration with Jay Allen’s MT-Blacklist.
  • 3.0 May 1 2003
    Changed hack to work for all MT backends, including DB_File.
  • 2.0 April 9 2003
    In addition to comment text, also compare author name, email and URL.
  • 1.0 April 9 2003
    Initial hack, only compared comment text.