9

I am on D7.

I have a content type that is webform enabled.

Requirement I need to implement captcha for all the webforms for nodes of this content type.

Problem In D7, you are supposed to give the form_id in the settings page to get captcha in the form, however I will have loads of forms with all different form_ids.

I am using the Captcha module.

Is there any way to implement my requirement?

3 Answers 3

9

You could add the following code to your template.php

if (strstr($form_id, 'webform_client_form')) {
        $form['my_captcha_element'] = array(
            '#type' => 'captcha',
            '#captcha_type' => 'image_captcha/Image',
        );
    }

this should be placed inside the yourthemename_form_alter

2
  • Thanks. I just got this solution, got it working, came back to answer my own question and saw that you already have. :) Thanks for the help nonetheless.
    – SGhosh
    Commented Jan 17, 2013 at 7:17
  • To have your custom code respect the permissions set for those people who can skip CAPTCHA add the following to your logic: if ((strstr($form_id, 'webform_client_form')) && !(user_access('skip CAPTCHA'))) { $form['my_captcha_element'] = array( '#type' => 'captcha', '#captcha_type' => 'default', ); }
    – kbrinner
    Commented Apr 17, 2015 at 21:49
1

Alternatively you can put this code in a custom module:

/**
* Implementation of hook_form_alter().
*/
function mymodule_form_alter(&$form, $form_state, $form_id) {
  if (preg_match("/^webform_client_form_[0-9]+$/",$form_id) && user_is_logged_in() == FALSE) {
    $form['my_captcha_element'] = array(
      '#type' => 'captcha',
    );
  }
}

Source: https://drupal.org/node/255795

1

To have your custom code respect the permissions set at admin/people/permissions for those people who can skip CAPTCHA, add the following to your logic:

if ((strstr($form_id, 'webform_client_form')) && !(user_access('skip CAPTCHA'))) {
    $form['my_captcha_element'] = array(
      '#type' => 'captcha',
      '#captcha_type' => 'default',
    );
  }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.