Download the PHP package jub/craft-google-recaptcha without Composer
On this page you can find all versions of the php package jub/craft-google-recaptcha. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download jub/craft-google-recaptcha
More information about jub/craft-google-recaptcha
Files in jub/craft-google-recaptcha
Package craft-google-recaptcha
Short Description Google reCAPTCHA for Craft CMS
License MIT
Informations about the package craft-google-recaptcha
Google reCAPTCHA plugin for Craft CMS 4
Google reCAPTCHA for Craft CMS enables to render and validate the reCAPTCHA widget. It is compatible with both API v2 and v3, including checkbox badge and invisible flavors.
Requirements
This plugin requires Craft CMS 4.0.0 or later and PHP 8.0.2 or later.
To use the plugin, you will need to get an API site key and secret key which you can configure here.
Installation
- Install with composer via
composer require jub/craft-google-recaptcha
from your project directory or from the Plugin Store section of the Control Panel. - Install the plugin in the Craft Control Panel under Settings → Plugins, or from the command line via
./craft plugin/install google-recaptcha
.
Configuration
Control Panel
You can manage configuration setting through the Control Panel by going to Settings → Google reCAPTCHA
- Provide the Site Key and the Secret Key obtained from your reCAPTCHA account.
- Select the API version accordingly to the reCAPTCHA type you created the keys upon.
- For v2 API, select the following parameters:
- Size: Select the size of the reCAPTCHA widget.
- Theme: Select the color theme of the reCAPTCHA widget.
- Badge: Select the position of the reCAPTCHA badge.
- For v3 API:
- Default Action: the default action name to be used during reCAPTCHA verification. Defaults to
homepage
if blank. - Default Score Threshold : Minimum score between 0 and 1 to obtain in order for the end user to validate the reCAPTHCHA challenge (see here for help on interpreting the score) . Leave blank for no score checking.
- Actions: A score threshold can be defined per action here.
- Default Action: the default action name to be used during reCAPTCHA verification. Defaults to
Configuration file
You can create a google-recaptcha.php
file in the config
folder of your project and provide the settings as follow:
⚠️ Any value provided in that file will override the settings from the Control Panel.
Using Google reCAPTCHA
Display Google reCAPTCHA widget in template
You can integrate the Google reCAPTCHA widget in your Twig templates as follow:
The render
method accept an optional parameter in which you can provide any HTML attributes to apply to the widget container (div for v2, hidden input for v3).
For example, to provide a container id, you can do:
For v3 API, an action property can also be provided as follow:
In that case, the score threshold to be used for that action can be defined in the "Actions Settings" part of the plugin control panel.
💡 For v2 API, you can provide a second boolean argument to the render method to trigger the instant rendering of the widget (ie.
{{ craft.googleRecaptcha.render({ id: 'recaptcha-widget' }, true) }}
). This is useful if you are working with views loaded through Ajax or Sprig calls and you need to refresh the widget.
Setting scripts extra attributes
In the first render parameter, scriptOptions
special property can be used to add extra attributes to the generated scripts tags.
For example, to support Content Security Policy (CSP), assuming you are using the Sherlock security plugin, you could do the following:
Verify users submissions
To validate a user submission on server side, you can use the built-in method:
For example, in a module controller, you could do something like this:
Verify Guest Entries submissions
In order to add a reCAPTCHA verification when working with Craft Guest Entries plugin, you can do something like the following in a project module:
Verify Contact Form submissions
In order to add a reCAPTCHA verification when working with Contact Form, you can do something like the following in a project module:
Available events
- The
\juban\googlerecaptcha\events\BeforeRecaptchaVerifyEvent
event is triggered just before a reCAPTCHA verification is performed. You can use that event to:- Bypass the verification by setting the
skipVerification
event property totrue
. In that case, verification will be considered as successful. - Cancel the verification by setting the
isValid
event property tofalse
. In that case, verification will be considered as failed.
- Bypass the verification by setting the