Last updated November 19, 2014. Created on August 19, 2011.
Edited by sfbob, Bob Rooney, dan3h, juanjo_dv. Log in to edit this page.

Drupal 6

  1. Go to Administer > User Management > CAPTCHA and check: Add CAPTCHA administration links to forms.
  2. Create the Webform. Do not click on the CAPTCHA link at the bottom of the Form Create page.
  3. Save the form.
  4. Click CAPTCHA: no challenge enabled at the bottom of the newly created form (when Viewing, not editing the form).
  5. Click Place a challenge here for untrusted users.
  6. Done.

Drupal 7

  1. Go to /admin/config/people/captcha
  2. In the FORM PROTECTION area
    in the configuration table
    go to the last row (with the empty FORM_ID entry field)
  3. Enter the information for this form

    In the FORM_ID column enter the form_id

    To quickly find the form_id of the webform
    take the node id and append it to "webform_client_form_"
    for example if the node id is 3 your webform form_id will be "webform_client_form_3".
    You can also use firebug to inspect the form_id field.

    In the CHALLENGE TYPE column make a selection from the dropdown box
  4. Scroll to the bottom of the page and click the "Save Configuration" button

If you want to programatically add captcha protection to all webforms you have to write a module which implements hook_entity_insert, hook_entity_update and hook_entity_delete.

Method 1

<?php
/**
 * Implements hook_entity_insert().
 */
function MYMODULE_entity_insert($entity, $type) {
 
_MYMODULE__set_webform_captcha('default', $entity, $type);
}
/**
 * Implements hook_entity_update();
 */
function MYMODULE_entity_update($entity, $type) {
 
_MYMODULE__set_webform_captcha('default', $entity, $type);
}
/**
 * Implements hook_entity_delete().
 */
function MYMODULE_entity_delete($entity, $type) {
 
_MYMODULE__set_webform_captcha(NULL, $entity, $type);
}
/**
 * Helper function to set (or unset) a captcha whenever any webform is changed.
 */
function _MYMODULE__set_webform_captcha($captcha_type, $entity, $entity_type) {
  if (
module_exists('webform') && module_exists('captcha') && !empty($entity->webform)) {
    list(
$webform_id) = entity_extract_ids($entity_type, $entity);
   
module_load_include('inc', 'captcha');
   
$form_id = 'webform_client_form_' . $webform_id;
   
captcha_set_form_id_setting($form_id, $captcha_type);
  }
}
?>

Method 2 (confirmed that this works)

In template.php add the following

<?php
function MYTHEME_form_alter( &$form, &$form_state, $form_id ){
    if (
strstr($form_id, 'webform_client_form')) {
           
$form['my_captcha_element'] = array(
               
'#type' => 'captcha',
               
'#captcha_type' => 'Math',
            );
        }
}
?>

Two things to note.

1) MYTHEME_form_alter must be changed to the name of your theme. Don't just copy and paste :)

2) captcha_type can be set to the following:

'default', 'none', 'captcha/Math', 'image_captcha/Image'

Original solution here: http://drupal.stackexchange.com/questions/57092/drupal-7-captcha-in-all-...

Looking for support? Visit the Drupal.org forums, or join #drupal-support in IRC.

Comments

Vojta’s picture

Is it possible to protect by CAPTCHA all webforms? By this I can add CAPTCHA only for one page, right?

Marco Piccolino’s picture

Are instructions at the top for Drupal 6? If so please make it clear.

Thanks!

VVS’s picture

How programmatically add webform to "Form Protection"? Not for all webforms (as hook).

erbeck’s picture

I'm having trouble with this for some reason. Trying to implement the instructions for Drupal 7. I'm working on the following webform: http://lawlibrary.colorado.edu/node/108/. I add the following Form ID to the list in CAPTCHA settings: webform_client_form_108, and I choose the challenge type of "image." No change to the webform. I clear the CAPTCHA cache, not a thing. I don't think there's anything unusual with the way the webform is configured. I'm using CAPTCHA 7.x-1.1 and Webform 7.x-3.20, which to my knowledge are both up-to-date. Anybody have any advice about this?

tregismoreira’s picture

Hey guys. I created a simple module that implements this code: https://www.drupal.org/project/captcha_webform. Maybe it will be more user friendly than coding a new module.