In the CAPTCHA branch 6.x-2.x, the CAPTCHA stuff was refactored to a real drupal form element, which makes things much cleaner, more in line with drupal standards and makes it easier for other modules to integrate with the CAPTCHA module.
This should be documented better somewhere (e.g. in a README file or a drupal.org handbook page).
For the time being we can iterate here a bit on this documentation.
Adding a CAPTCHA element to a form is as easy as adding a text field or option list (maybe even easier):
$form['my_captcha_element'] = array(
'#type' => 'captcha',
);
This adds a default CAPTCHA element to the form.
The CAPTCHA type can be set explicitly too:
$form['my_captcha_element'] = array(
'#type' => 'captcha',
'#captcha_type' => 'captcha/Math',
);
Comments
Comment #1
soxofaan CreditAttribution: soxofaan commentedFor reference, some threads talking/asking for this type of functionality:
#57781: Hook to let any module use captchas
#536274: Show Captcha After X Unsuccessful Attempts
#641122: Integrate with Spam module
Comment #2
a5342346 CreditAttribution: a5342346 commentedHi,
I'm building a standalone login page, with a custom-themeable LoginForm. I'm following instructions at,
"Theming the Drupal 6 User Login Form"
http://blog.aphexcreations.net/2009/04/theming-drupal-user-login-form.html
I've created an override "user-login.tpl.php"
as well as a standalone LoginPage (page-login.tpl.php), in which I output content, as usual, with
Which nicely outputs a functional login form -- but WITHOUT the display of my Drupal6 CAPTCHA module's reCaptcha element. The module's installed OK, and functions fine -- if/when display in the NOT-overriden, standard Login form.
What I'd like to do is include the reCaptcha element in MY form.
To do that, as a first step, I added the snippet from above to my theme's template.php as referenced in the link/tutorial above, i.e.,
But, that's apparently not correct/sufficient -- I simply see my original/custom form, with no added (re)Captcha "Math" challenge visible, and nothing Captcha-related in the generated PageSource.
What's the right method to get the Captcha element added to "my" customizable form?
Thanks!
BenDJ
Comment #3
a5342346 CreditAttribution: a5342346 commentedgot it figured out.
in addition to
the function in template.php needs
and then, the user-login.tpl.php needs,
AFter cache clear, the recaptcha element displays as expected.
Comment #5
a5342346 CreditAttribution: a5342346 commentedi spoke a little too soon ...
the code above _displays_ the (re)captcha on my custom login page just fine.
but, the (re)captcha is _not_ part of the login validation flow -- username/password, with NOT (re)captcha entry/submit -- is sufficient to login. i.e., the (re)captcha's displayed, but ignored.
i can, i suppose, manually code an 'is_valid' check on the captcha response, but how would/should that be done still using the (re)captcha module's hooks?
Comment #6
soxofaan CreditAttribution: soxofaan commentedDoes it work with the simple math CAPTCHA? (reCAPTCHA is a bit of a special case and the math CAPTCHA is easier for debugging)
Comment #7
a5342346 CreditAttribution: a5342346 commentedi'm taking a different approach -- using a custom module, and working with #redirect.
so, for now, n/a.
thanks then.
Comment #8
a5342346 CreditAttribution: a5342346 commentedComment #9
soxofaan CreditAttribution: soxofaan commentedPlease don't close this thread as the original issue is not fixed yet: better documentation on how to add a CAPTCHA widget programmatically
Comment #10
a5342346 CreditAttribution: a5342346 commentedsry, my mistake. thought it was one of my threads, and i don't know of any other way to stop following an issue ...
Comment #11
joyseeker CreditAttribution: joyseeker commentedI want to add the CAPTCHA to a Profile form. There's no "markup" component like in Webform, so where would I put the PHP from the initial description above?
Comment #12
soxofaan CreditAttribution: soxofaan commentedat joyseeker in #11: you don't need any PHP discussed here
adding a CAPTCHA to a profile form is possible out of the box with the CAPTCHA module:
* enable the option "add CAPTCHA administration links to forms" on the CAPTCHA admin page
* go to the profile form you want to add a CAPTCHA to, and use the (collapsed) CAPTCHA administration fieldset over there.
Comment #13
joyseeker CreditAttribution: joyseeker commentedto #12
Thanks so much for taking the time to answer me! Yes, I now have the CAPTCHA on my Profile form.
Comment #14
white_pawn CreditAttribution: white_pawn commentedAny way we could change the reCAPTCHA theme before/after loading 'my_captcha_element'?
Comment #15
soxofaan CreditAttribution: soxofaan commentedfor reference: #966128: Documentation needs work (D6 and D7)
Comment #16
Simon Naude CreditAttribution: Simon Naude commentedI added this to my custom registration form. It seems to work. Thanks for the code.
Comment #17
ransomweaver CreditAttribution: ransomweaver commentedI have an exposed filter on a view that needs a captcha, but it appears I can't enable it from the Captcha settings. I actually have two exposed filters, and they both appear in drupalforfirebug as form>views_exposed_form.
Adding the form in hook_form_alter using
$form['contact_wrapper']['info_captcha_element'] = array(
'#type' => 'captcha',
'#weight' => 35,
);
places the captcha form where I want it, but the page loads initially displaying :
* Math question field is required.
* The answer you entered for the CAPTCHA was not correct.
And when I submit I get: The answer you entered for the CAPTCHA was not correct.
Anyone know what is up with this? The form is in an expanded fieldset.
Comment #18
soxofaan CreditAttribution: soxofaan commented@ransomweaver: indeed, I could reproduce the your issue (with the standard way to add CAPTCHA to a form, not programmatically in form_alter). I think it's best to open a separate issue/ticket for this one. It seems an exposed view filter form is automatically submitted (before you get the chance to fill it in) in order to show a default view.
Comment #19
ransomweaver CreditAttribution: ransomweaver commentedThanks, new issue is #1064158: Captcha added to views exposed filter form gets automatically submitted, resulting in captcha error
Comment #20
josepvalls CreditAttribution: josepvalls commentedWhat about image_captcha
It's probably trivial, but I can't figure it out.
update: I found it.
Comment #21
-enzo- CreditAttribution: -enzo- commentedIf you need add an image captcha for a specific content type like webform, you can use the following code
Comment #22
cknoebel CreditAttribution: cknoebel commented#21 works great. I'd add that it should go into a custom module using hook_form_alter:
Comment #23
typehost CreditAttribution: typehost commentedIn Drupal 7 and the theme I am working with:
in template.php ---> added:
and in user-login.tpl.php:
This worked fine for the user login page, but for some reason captcha was still not displaying on the user registration page where most of the spam comes from - despite the fact that it had no custom template or modifications and should have worked through admin config options on the captcha module (user_register_form). I added a hook form alter function in template.php to correct that.
final code in template.php:
Final code in user-login.tpl.php:
Comment #24
soxofaan CreditAttribution: soxofaan commented@typehost in #23: I'm afraid that adding CAPTCHA in the theme layer/phase is too late to work properly. Have you tried already posting a form with wrong CAPTCHA response?
Comment #25
jive01 CreditAttribution: jive01 commentedFor Hidden Captcha:
Comment #26
maximkashubaFor Draggable Captcha:
Comment #27
mnask CreditAttribution: mnask commentedhow can i find the value(i.e 3+4=) the user has to enter ?
Comment #28
Miri Meltzer CreditAttribution: Miri Meltzer as a volunteer commented// Set captcha image for specific webform form.
module_load_include('inc', 'captcha');
$form_id = 'webform_client_form_' . $my_form_id();
$captcha_type = 'image_captcha/Image';
captcha_set_form_id_setting($form_id, $captcha_type);
Comment #29
wundo CreditAttribution: wundo at Chuva Inc. for Galoa Science commentedComment #30
wundo CreditAttribution: wundo at Chuva Inc. for Galoa Science commentedAdding a CAPTCHA programmatically nowadays it's just about exporting the config entity properly, marking this as fixed.
Comment #31
wundo CreditAttribution: wundo at Chuva Inc. for Galoa Science commented