An extension to the Kirby 2 plugin Uniform (v3) to add the Google reCAPTCHA guard.
Get the Kirby CLI and run kirby plugin:install fetzi/kirby-uniform-recaptcha
.
Download the repository and extract it to site/plugins/uniform-recaptcha
.
Run composer require fetzi/kirby-uniform-recaptcha
. Then add the second require
to the index.php
like this:
// load kirby
require(__DIR__ . DS . 'kirby' . DS . 'bootstrap.php');
require 'vendor'.DS.'autoload.php';
Be sure to include the new vendor
directory in your deployment.
Define the mandatory config settings:
c::set('uniform-recaptcha-sitekey', 'YOUR RECAPTCHA SITEKEY');
c::set('uniform-recaptcha-secret', 'YOUR RECAPTCHA SECRET');
You can use the recaptchaGuard
in your controllers form definition:
$form = new Form(/* ... */);
if (r::is('POST')) {
$form->recaptchaGuard()
->emailAction(/* ... */);
}
To embed the recaptcha field into your template simply call
<?php echo recaptcha_field(); ?>
The plugin needs the recaptcha Javascript File provided by Google. You can either include the JavaScript file directly into your page or by calling the method embed_recaptcha_js()
in your template.
Example
<form action="<?php echo $page->url()?>" method="post">
<label for="name" class="required">Name</label>
<input<?php if ($form->error('name')): ?> class="erroneous"<?php endif; ?> name="name" type="text" value="<?php echo $form->old('name') ?>">
<!-- ... -->
<?php echo csrf_field() ?>
<?php echo recaptcha_field() ?>
<input type="submit" value="Submit">
</form>
<?php echo embed_recaptcha_js(); ?>
Johannes Pichler https://johannespichler.com