I'm looking to add captcha support to the webform module, but it doesn't look like there's any easy way to append items to the list of captcha_points (other than editing the module source!).

Could a hook be added into captcha to append other module forms? Something like this in the captcha module:

    $newpoints = module_invoke_all("captcha_add");
    foreach ($newpoints as $key=>$value) {
      $captcha_points[$key] = $value

And then in the any other module (such as webform, for instance) put:

    function webform_captcha_add () {
       return array (
         'webform_client' => t('Webform Client Form'),
         'webform_someotherform' => t('Some Other Webform Form'),

I'm looking forward to adding this much requested feature to webform, thanks!


quicksketch’s picture

Version: 4.6.x-1.x-dev » 6.x-2.x-dev
kulvik’s picture

I'd also like something like this. I really need to use captcha with my guestbook module. It fille up with spam daily :/

quicksketch’s picture

Here's a bit of a work-around for this issue. I've found that more of a change would be necessary than I originally thought. What if a module wanted to customize the captcha, rather than having the captcha module simply stick the captcha at the bottom of the form? A seperate function would be needed that would return only the captcha and skips all the form-specific code currently in the captcha_form_alter() function.

In the exisiting system, we can still accomplish what we want by copying some of the captcha code into the custom module and passing in a fake form item to the captcha module, then merging the result with our custom module form. That sounds more complicated than it is. Here's the code:

if (module_exist("captcha")) {
	// Create a dummy form to pass to the captcha function
	$formItem = array();
	// Pass the form to the captcha rendering function
	$captcha_type = variable_get("captcha_type", NULL);
	if (module_hook($captcha_type, 'captchachallenge')) {
		call_user_func_array($captcha_type.'_captchachallenge', array(&$formItem, &$_SESSION['captcha']));   
        // Add the captcha to the actual form
	$form['captcha_response'] = $formItem['captcha_response']; 

        // Change any settings to the captcha
        $form['captcha_response'] ['#weight'] = 2;
arnabdotorg’s picture

The concept of a "captcha point" exists in the cvs version, which uses an array to define what forms you want to use captcha for. One option is to have a "captchapoint" hook, in which every module returns what all points you could have captchas. this is added to the list of captcha points, and this will show up in the captcha config.

arnabdotorg’s picture

ugh, i just noticed the original post says more or less the same thing i wrote. i should go sleep now.

wundo’s picture

Assigned: Unassigned » wundo

This is a Great feature, I will work in it today... Then I post my solutions here to see what you think ;)

kowalke’s picture

I don't suppose there could be a patch for this that one could apply to a 4.6 install? I'm not quite able to upgrade to 4.7 yet, but I desperately need captcha on my feedback form (which I hear isn't a problem with 4.7).

wundo’s picture

Status: Active » Closed (fixed)
donquixote’s picture

Status: Closed (fixed) » Active

This doesn't appear to be fixed .. or otherwise, it needs some documentation.

+1 for this feature request!
In particular, I would like to automatically integrate with webform.

soxofaan’s picture

Component: Code » Captcha API (captcha)
Assigned: wundo » Unassigned
Status: Active » Fixed


This is a bit of an old/outdated thread, so this might not be the best place to discuss this (e.g. strange thing in #1: the version was changed from 4.6.x-1.x-dev to 6.x-2.x-dev, but I doubt that even Drupal 6 existed in 2006)

Anyway, this might be the function you are looking for (first function in captcha.inc):

 * Helper function for adding/updating a CAPTCHA point.
 * @param $form_id the form ID to configure.
 * @param captcha_type the setting for the given form_id, can be:
 *   - 'none' to disable CAPTCHA,
 *   - 'default' to use the default challenge type
 *   - NULL to remove the entry for the CAPTCHA type
 *   - something of the form 'image_captcha/Image'
 *   - an object with attributes $captcha_type->module and $captcha_type->type
 * @return nothing
function captcha_set_form_id_setting($form_id, $captcha_type) {

Setting this thread back to fixed, unless someone disagrees :)

donquixote’s picture

Status: Fixed » Active

The point is that I would like to be a bit more independent of the form id.
Webform form ids depend on the node id, and I really don't like code that depends on node ids.

I am looking for something that can be called in hook_form_alter, or in a form builder function. Or maybe even a #process handler that can be attached to the form.

I am sure one could come up with more ideas..
If you know a better place to discuss this, let me know!

soxofaan’s picture

ok, then you are probably interested in this part of #641122: Integrate with Spam module:

... But the CAPTCHA branch 6.x-2.x is the result of major refactoring to make it possible for other modules to inject CAPTCHAs in the way they want. CAPTCHA 6.x-2.x defines a CAPTCHA element that can be added to forms just like text fields an option lists.
The core CAPTCHA module just provides a basic admin interface that indeed stores its captcha points in its database table.

Adding a CAPTCHA to a form is just as easy as:

$form['my_captcha'] = array(
  '#type' => 'captcha',

But true, this should be documented better

soxofaan’s picture

Status: Active » Closed (fixed)

But true, this should be documented better

Made a new issue about this: #743056: Document how to add a CAPTCHA programmatically
I think it's best to continue the discussion over there, and close this issue again because it's mainly from the Drupal 4.7 era.