Adding CAPTCHA to a Webform

Last updated on
30 April 2025

You are browsing documentation for an older version of Drupal, which is not supported any longer, and the information may not be correct. See current documentation for Contributed modules

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' => 'captcha/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-...

Method 3

Another approach is to enable the CAPTCHA via the CTools API by adding this to a custom module:

/**
 * Implementation of hook_captcha_default_points_alter().
 *
 * Enable CAPTCHA integration for all Webforms.
 */
function MYMODULE_captcha_default_points_alter(&$items) {
  // Only proceed if the Webform module is enabled.
  if (module_exists('webform')) {
    // Get the list of supported Webform content types.
    $webform_types = webform_node_types();
    if (!empty($webform_types)) {
      // Get a list of all nid's for all supported webform content types.
      $webforms = array();
      $nodes = db_select('node')
        ->fields('node', array('nid'))
        ->condition('type', $webform_types)
        ->execute();
      foreach ($nodes as $node) {
        // Webform form IDs follow this format.
        $webforms[] = 'webform_client_form_' . $node->nid;
      }

      // Enable CAPTCHA integration for each form, if it isn't already enabled.
      foreach ($webforms as $form_id) {
        if (!isset($items[$form_id])) {
          $captcha = new stdClass;
          $captcha->disabled = FALSE;
          $captcha->api_version = 1;
          $captcha->form_id = $form_id;
          $captcha->module = '';
          // This may be 'node', 'default', 'captcha/Math',
          // 'recaptcha/reCAPTCHA', etc.
          $captcha->captcha_type = 'default';
          $items[$form_id] = $captcha;
        }
      }
    }
  }
}

Help improve this page

Page status: Not set

You can: