ArchiveArchive

Add reCaptcha to your Drupal 7 forms programatically

Oct 25, 2016 · Updated: Nov 04, 2017 · by Tim Kamanin

If you want to add Google's reCaptcha (https://www.google.com/recaptcha/intro/index.html) to your Drupal 7 forms programmatically you need to follow these two steps:

1) Install and enable captcha (https://www.drupal.org/project/captcha) and recaptcha (https://www.drupal.org/project/recaptcha) modules. The best way to do this is to use drush: drush en -y recaptcha

2) Add this snippet to any Drupal form's code you wish to have this captcha on:

$form['captcha'] = array(
  '#type' => 'captcha',
  '#captcha_type' => 'recaptcha/reCAPTCHA',
);

And you're done! I hope from now on, spam bots will bother you less.

Don't forget to share this little tip if you like it!

Hey, if you've found this useful, please share the post to help other folks find it:

There's even more:

Subscribe for updates

  • via Twitter: @timonweb
  • old school RSS:
  • or evergreen email ↓ ↓ ↓