# Kirby reCaptcha

**URL:** <https://forum.getkirby.com/t/kirby-recaptcha/4631>\
**Category:** Plugins\
**Tags:** discontinued\
**Created:** [July 7, 2016, 9:33am UTC](https://forum.getkirby.com/t/kirby-recaptcha/4631 "2016-07-07T09:33:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jenstornell](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/jenstornell/32/546_2.png) [@jenstornell](https://forum.getkirby.com/u/jenstornell)\
**Post date:** [July 7, 2016, 9:33am UTC](https://forum.getkirby.com/t/kirby-recaptcha/4631/1 "2016-07-07T09:33:26Z")

</div>

[https://github.com/jenstornell/kirby-recaptcha](https://github.com/jenstornell/kirby-recaptcha)

# Kirby reCaptcha

Validate Google reCaptcha and do something with a PHP and a javascript callback. Some basic coding skills required.

 ![](https://europe1.discourse-cdn.com/flex017/uploads/getkirby/original/2X/e/e8cbc4e72e1839afb613d4a3ca25244e1260a481.gif)

## Install

### Kirby CLI

Run this command:

```auto
kirby plugin:install jenstornell/kirby-recaptcha

```

### Manually

Add the folder `kirby-recaptcha` into `/site/plugins/`.

## Setup

### 1. Register reCaptcha

Go to [recaptcha](https://www.google.com/recaptcha/admin) and register the domain you want to use.

### 2. Setup config

You need to add `secret` and `sitekey` to your config.

```php
c::set('plugin.recaptcha.secret', 'your-secret');
c::set('plugin.recaptcha.sitekey', 'your-sitekey');

```

### 3. Add javascript files

Add this to your `footer.php` snippet:

```php
<?php
	echo js('/assets/plugins/kirby-recaptcha/js/script.js');
	echo js('https://www.google.com/recaptcha/api.js?hl=sv');
?>

```

It should be placed just before `</body>`.

1. The first file is a setup for this Kirby plugin.
2. The second file is a setup from Google.

The `sv` language code can be changed to [your language](https://developers.google.com/recaptcha/docs/language).

## Usage

### 1. Add a html form

Make sure it uses `method` that is `post`.

```html
<form class="my-form" action="" method="post">
	<label for="name">Name:</label>
	<input name="name">

	<div class="g-recaptcha" data-sitekey="<?php echo c::get('plugin.recaptcha.sitekey'); ?>"></div>

	<input type="submit" value="Submit" />
</form>

```

### 2. Add your PHP callback as a plugin

Add a PHP callback into a plugin.

The PHP callback will make you do something when the captcha is successful. You have access to `$post` and `$response`.

```php
function recaptchaCallback( $post, $response) {
	print_r( $post );
	print_r( $response );

	if( $post['g-recaptcha-selector'] == '.my-form') {
		if( $response['success'] ) {
			$array = array(
				'success' => true,
			);
		} else {
			$array = array(
				'success' => false,
				'message' => 'Failed!'
			);
		}
		return json_encode($array);
	}
}

```

- The function name has to be `recaptchaCallback`.
- In the `$post` the selector is included as well, called `g-recaptcha-selector`.
- It should be placed just before `</body>`.

### 3. Add your javascript callback

This script is to run the reCaptcha.

```html
<script>
document.addEventListener("DOMContentLoaded", function() {
	recaptcha.init({
		url: '<?php echo u(); ?>',
		selector: '.my-form',
		callback: function(xhr) {
			console.log(xhr);
		}
	});
});
</script>

```

**Url**

To make the javascript know about the route you need to send the root url.

**Selector**

Your selector to the html form like above is `.my-form`.

**Callback**

The callback can be used to get the `xhr` object. From that you can see if the captcha was successful or not.

## Requirements

- Kirby 2.3
- Google Account

## License

- Kirby reCaptcha - MIT
- Google Recaptcha - [BSD](http://github.com/google/recaptcha/blob/master/LICENSE)

---

<div class="post-metadata">

**Author:** ![jenstornell](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/jenstornell/32/546_2.png) [@jenstornell](https://forum.getkirby.com/u/jenstornell)\
**Post date:** [August 24, 2016, 1:23pm UTC](https://forum.getkirby.com/t/kirby-recaptcha/4631/2 "2016-08-24T13:23:44Z")

</div>

## Version 0.2 is here:

**0.2**

- Added namespaces
- Making it easier and more correct than before
- Code enhancements

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [April 16, 2017, 6:19pm UTC](https://forum.getkirby.com/t/kirby-recaptcha/4631/3 "2017-04-16T18:19:18Z")

</div>

Looks like this doesn’t exist anymore…

---

<div class="post-metadata">

**Author:** ![bogi30](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@bogi30](https://forum.getkirby.com/u/bogi30)\
**Post date:** [August 24, 2020, 8:43am UTC](https://forum.getkirby.com/t/kirby-recaptcha/4631/4 "2020-08-24T08:43:23Z")

</div>

It seems that reCaptcha is no longer alive in Kirby. For a new web site, I want to implement a simple and still supported Captcha solution for a contact form in Kirby. As this thread is showing up on top in google search results, maybe some of the experienced users could point to some advisable solutions?  
I think, this could help also other Kirby users.  
Personally, I would be interested for the reasons behind the discontinuation.

Regards,  
Bogi
