Thursday, September 27, 2012

How to Disable Home Page textbox in Drupal Comments Form

Tired of automated spam comments for backlinks? To keep my life easy to manage Drupal sites I've finally come up with a smallest module that I'm most happy with.

It offers two functionalities:

  1. Removes home page from comment form
  2. Disables typing http:// or https:// within the comment text

Guess now what is left for the bots to try on your site! Well I still found 1 or 2 spams just testing without backlinks if they could post at all.

But life is much better for me managing a dozen sites.

Code

This is for D6. Create folder "anu_disable_http" in sites/all/modules and create these files

anu_disable_http.info

; $Id: anu_disable_http.info,v 1.1 2012/09/26 09:08:13 D14 Exp $
name = Anu Disable http in comments
description = to prevent spam
files[] = anu_disable_http.module
core = 6.x
package = custom
; Information added by drupal.org packaging script on 2010-11-12
version = 1.0
core = 6.x

anu_disable_http.module

function anu_disable_http_form_alter(&$form, &$form_state, $form_id) {
global $user;
if ($user->uid !=1 and $form_id == 'comment_form') {
$form['#validate'][] = 'anu_disable_http_validate_comments'; if(isset($form['homepage']))
{
$form['homepage']['#access'] = FALSE; //disable homepage
} }
} function anu_disable_http_validate_comments($form, $form_state)
{
$pattern = '/\bhttps?:\/\//i';
if(preg_match($pattern,$form_state['values']['comment']))
{
form_set_error('comment',"Please remove http:// or https:// from links, we will add them if required");
}

Enable it

Go to admin/build/modules and enable this and enjoy.

No comments:

Post a Comment