PHPackages                             alexstack/google-recaptcha-to-any-form - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Validation &amp; Sanitization](/categories/validation)
4. /
5. alexstack/google-recaptcha-to-any-form

ActiveLibrary[Validation &amp; Sanitization](/categories/validation)

alexstack/google-recaptcha-to-any-form
======================================

Display a Google Recaptcha v2 or v3 or invisible in any custom form with flexible settings and no affection to your existing code. Also works well for SilverStripe 4.x/3.x/2.x &amp; Laravel &amp; Wordpress &amp; other CMS.

1.4.0(6y ago)67.8k3[1 issues](https://github.com/AlexStack/Google-Recaptcha-to-any-form/issues)1MITPHPPHP &gt;=5.0

Since Jun 26Pushed 6y agoCompare

[ Source](https://github.com/AlexStack/Google-Recaptcha-to-any-form)[ Packagist](https://packagist.org/packages/alexstack/google-recaptcha-to-any-form)[ Docs](https://github.com/AlexStack/Google-Recaptcha-to-any-form)[ RSS](/packages/alexstack-google-recaptcha-to-any-form/feed)WikiDiscussions master Synced today

READMEChangelog (6)DependenciesVersions (8)Used By (1)

Google Recaptcha v2 or v3 to any form
=====================================

[](#google-recaptcha-v2-or-v3-to-any-form)

[![Latest Stable Version](https://camo.githubusercontent.com/ee13e3e2a3468a78d8b7a5ceea10ee711e2be57a4b9687a0d00051cd19ab71b7/68747470733a2f2f706f7365722e707567782e6f72672f616c6578737461636b2f676f6f676c652d7265636170746368612d746f2d616e792d666f726d2f762f737461626c65)](https://packagist.org/packages/alexstack/google-recaptcha-to-any-form)[![License](https://camo.githubusercontent.com/ec632460e50e099d4e33ded3abcbafaa2353d0ed6fd3267a06aea56756169dd1/68747470733a2f2f706f7365722e707567782e6f72672f616c6578737461636b2f676f6f676c652d7265636170746368612d746f2d616e792d666f726d2f6c6963656e7365)](https://packagist.org/packages/alexstack/google-recaptcha-to-any-form)[![Total Downloads](https://camo.githubusercontent.com/c20ce0e24c0b6c4a84909a24d1a0df6f92289ab1af8f422ee9e4c27f989473ec/68747470733a2f2f706f7365722e707567782e6f72672f616c6578737461636b2f676f6f676c652d7265636170746368612d746f2d616e792d666f726d2f646f776e6c6f616473)](https://packagist.org/packages/alexstack/google-recaptcha-to-any-form)

- It can display a Google Recaptcha v2 or v3 or invisible in any custom form with flexible settings and no affection to your existing code. Also works well for SilverStripe 4.x/3.x/2.x &amp; Larave &amp; Wordpress &amp; other CMS.

Basic example codes
===================

[](#basic-example-codes)

- Display Google Recaptcha v2 or v3 after a Form\_Field\_ID:

```
// For Google recaptcha v2
GoogleRecaptcha::show('SiteKey', 'Form_Field_ID');

// For Google recaptcha v3
GoogleRecaptcha::showV3('SiteKey', 'Form_Field_ID');
```

- Verify it in the backend php:

```
GoogleRecaptcha::verify('SecretKey');
```

How to install
==============

[](#how-to-install)

```
composer require alexstack/google-recaptcha-to-any-form
```

Contents
========

[](#contents)

- [How to display it on frontend page](#frontend) - Support [Google recaptcha v3](#v3) / [v2](#frontend) / [Invisible](#invisible)
- [How to verify it in the backend script](#backend)
- [Usage example for SilverStripe 4.x/3.x](#silverstripe)
- [Usage example for Laravel](#laravel)
- [Usage example for Wordpress](#wordpress)

How to display it on frontend page?
=======================================================================

[](#how-to-display-it-on-frontend-page)

[![How to display it on frontend page](https://camo.githubusercontent.com/edea3ac49b0f8ecab9889986fb29c4c49b38740b899b39d2d32e61c5b9ec5cee/68747470733a2f2f646576656c6f706572732e676f6f676c652e636f6d2f7265636170746368612f696d616765732f6e657743617074636861416e63686f722e676966 "Google Recaptcha")](https://camo.githubusercontent.com/edea3ac49b0f8ecab9889986fb29c4c49b38740b899b39d2d32e61c5b9ec5cee/68747470733a2f2f646576656c6f706572732e676f6f676c652e636f6d2f7265636170746368612f696d616765732f6e657743617074636861416e63686f722e676966)

- Set up your Google Recaptcha v2 or v3 account for you website and get the site key and secret key
- Import the GoogleRecaptcha class:

```
use GoogleRecaptchaToAnyForm\GoogleRecaptcha;
```

- Put below php code in your frontend template/page for **Google Recaptcha v2**.

```
GoogleRecaptcha::show($SiteKey, 'Form_ContactForm_Message', 'no_debug', 'mt-4 mb-1', 'Please tick the reCAPTCHA checkbox first!');
```

[![google-recaptcha-v2-default](docs/images/google-recaptcha-v2-default.png "google-recaptcha-v2-default")](docs/images/google-recaptcha-v2-default.png)

### Explain for above code:

[](#explain-for-above-code)

- '$SiteKey': The Google Recaptcha Site Key of your website. You can directly put site key here or a variable or from database.
- 'Form\_ContactForm\_Message': Form\_Field\_ID, via html code. eg. ... `` ... Your Google Recaptcha will display after this form field.
- 'no\_debug': Change to debug and not tick the I'm not a robot checkbox will continue submit the form, then you will see the failed message.
- 'mt-4 mb-1': Extra css class name for the Google Recaptcha area. For recaptcha v2: Add theme-dark if you want the dark theme instead of the light. Add no-api-js if you already import the recaptcha/api.js.
- 'Please tick the reCAPTCHA checkbox first': Frontend alert message if the end user does not tick the checkbox. **Tips:** You can change this value to "v3", it will automatically switch to use Google Recaptcha v3
- Default value of the parameters of the show() method

```
show($site_key,$after_field_id='Form_ContactForm_Comment', $debug='no_debug', $extra_class="mt-4 mb-4", $please_tick_msg="Please tick the I'm not robot checkbox");
```

### How to use **Google Recaptcha Invisible**

[](#how-to-use-google-recaptcha-invisible)

- You need get Google Recaptcha Invisible site key and secret key first. It's different from v2
- Based on above explain, add a sting **invisible** to the Extra css class will do the job. eg. mt-4 invisible mb-1

```
GoogleRecaptcha::show($SiteKey, 'Form_Field_ID', 'no_debug','mb-2 invisible');
```

### Frontend example for **Google Recaptcha v3**

[](#frontend-example-for-google-recaptcha-v3)

```
GoogleRecaptcha::show($SiteKey, 'Form_Field_ID', 'no_debug','show-inline-badge mt-4 mb-3','v3');
// or
GoogleRecaptcha::showV3($SiteKey, 'Form_Field_ID', 'no_debug');
```

[![google-recaptcha-v3-default](docs/images/google-recaptcha-v3-default.png "google-recaptcha-v3-default")](docs/images/google-recaptcha-v3-default.png)

- Our Google Recaptcha v3 will automatically get g-recaptcha-response value after the page load 10 seconds or the Form\_Field\_ID(eg. Form\_ContactForm\_Message) was clicked.
- 'no\_debug': Change to "debug" will always submit an empty g-recaptcha-response to the backend.
- 'show-inline-badge mt-4 mb-3': Extra css class name for the Google Recaptcha inline-badge. Remove show-inline-badge will show a right bottom float Recaptcha badge instead inline-badge.

[![google-recaptcha-v3-inline](docs/images/google-recaptcha-v3-inline.png "google-recaptcha-v3-inline")](docs/images/google-recaptcha-v3-inline.png)

- You need set up Google Recaptcha v3 site key and secret key first. It's different from v2
- If you do not want to use the show() method, You can also use your own code to display the recaptcha for a custom style. Just make sure the form action method is POST, then you can still use below verify() method in your backend script.

How to verify it in the backend script
=========================================================================

[](#how-to-verify-it-in-the-backend-script)

- Import the GoogleRecaptcha class:

```
use GoogleRecaptchaToAnyForm\GoogleRecaptcha;
```

- Put below php code into the form-submitted-method() of your backend php file.

```
GoogleRecaptcha::verify($GoogleRecaptchaSecretKey, 'Google Recaptcha Validation Failed!!');
```

- Description for above code:
    - '$GoogleRecaptchaSecretKey': The Google Recaptcha Secret Key of your website. You can directly put secret key here or a variable or from database.
    - 'Google Recaptcha Validation Failed': Javascript alert message if the verification failed. Set it null or false if you don't want a javascript alert. It will return true or false by the Google recaptcha verification result. Then you can show your own error message.
- Default value of the parameters of the verify() method

```
verify($secret_key, $break_msg = null, $recaptcha_score=0.5)
```

- If you are using Google Recaptcha v3, it verify score &lt; 0.5 as a failed result by default. You can set the score if you want a different value. eg.

```
GoogleRecaptcha::verify($SecretKey, 'Google Recaptcha Validation Failed!!', 0.36);
```

Usage example for SilverStripe 4.x/3.x
==============================================================================

[](#usage-example-for-silverstripe-4x3x)

- Import the GoogleRecaptcha class:

```
use GoogleRecaptchaToAnyForm\GoogleRecaptcha;
```

- Create a function to display the recaptcha in your controller. eg.:

```
public function showGoogleRecaptcha()   {
    return GoogleRecaptcha::show($SiteKey, 'Form_ContactForm_Message', 'no_debug', 'mt-4 mb-1', 'Please tick the reCAPTCHA checkbox first!');
}
```

- Display the recaptcha in the frontend.ss form, add below code to the end of a frontend.ss template. eg. :

```
$showGoogleRecaptcha.RAW
```

- Verify the recaptcha in the controller php file, add below code to the formAction function of your controller. eg.:

```
GoogleRecaptcha::verify($GoogleRecaptchaSecretKey, 'Google Recaptcha Validation Failed!!');
```

Usage example for Laravel 5.x custom login form
==================================================================================

[](#usage-example-for-laravel-5x-custom-login-form)

- Include it in your LoginController.php file first:

```
use GoogleRecaptchaToAnyForm\GoogleRecaptcha;
```

- Create a function to display the recaptcha in your LoginController.php eg.:

```
public function showLoginForm()
{
    $showRecaptcha = GoogleRecaptcha::show('site_key', 'password', 'no_debug', 'mt-4 mb-3 col-md-6 offset-md-4', 'Please tick the reCAPTCHA checkbox first!');
    return view('auth.login', compact('showRecaptcha'));
}
```

- Display the recaptcha in the auth/login.blade.php, add below code to the end of the auth/login.blade.php template. eg. :

```
{!! $showRecaptcha !!}
```

- Verify the recaptcha in the LoginController.php file, add below code for validateLogin. eg.:

```
protected function validateLogin(Request $request)
{

    GoogleRecaptcha::verify('secret_key', 'Google Recaptcha Validation Failed!!');

    $request->validate([
        $this->username() => 'required|string',
        'password' => 'required|string',
    ]);
}
```

Usage example for Wordpress custom form
============================================================================

[](#usage-example-for-wordpress-custom-form)

- Include it in your custom form template php file first. Note: Change the correct vendor path for require\_once:

```
require_once(__DIR__ . '/../../../../vendor/autoload.php');
use GoogleRecaptchaToAnyForm\GoogleRecaptcha;
```

- Display the recaptcha in the form template. eg. :

```
echo GoogleRecaptcha::show('site_key', 'input_2_3', 'no_debug', 'mt-4 mb-3 col-md-6 offset-md-4', 'Please tick the reCAPTCHA checkbox first!');
```

- Verify the recaptcha in the handle form submission method . eg.:

```
require_once(__DIR__ . '/../../vendor/autoload.php');
use GoogleRecaptchaToAnyForm\GoogleRecaptcha;

GoogleRecaptcha::verify('secret_key', 'Google Recaptcha Validation Failed!!');
```

License
=======

[](#license)

- MIT

###  Health Score

32

—

LowBetter than 74% of packages

Maintenance17

Infrequent updates — may be unmaintained

Popularity29

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity58

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~13 days

Total

6

Last Release

2392d ago

PHP version history (2 changes)1.0.0PHP &gt;=5.0

1.1.0PHP ^5.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/901cd2118e154c3edde87b308ac1b95ce5b98ee88fedd0be3774d5961e71e389?d=identicon)[AlexStack](/maintainers/AlexStack)

---

Top Contributors

[![AlexStack](https://avatars.githubusercontent.com/u/33879126?v=4)](https://github.com/AlexStack "AlexStack (8 commits)")

---

Tags

googlerecaptcharecaptchasilverstripesilverstriperecaptchaGoogle ReCaptchagoogle recaptcha v2silverstripe3silverstripe4

### Embed Badge

![Health badge](/badges/alexstack-google-recaptcha-to-any-form/health.svg)

```
[![Health](https://phpackages.com/badges/alexstack-google-recaptcha-to-any-form/health.svg)](https://phpackages.com/packages/alexstack-google-recaptcha-to-any-form)
```

###  Alternatives

[karser/karser-recaptcha3-bundle

Google ReCAPTCHA v3 for Symfony

1872.3M6](/packages/karser-karser-recaptcha3-bundle)[undefinedoffset/silverstripe-nocaptcha

A spam protector and form field using Google's reCAPTCHA v2 or optionally a foundation v3 implementation

31465.3k16](/packages/undefinedoffset-silverstripe-nocaptcha)[sheadawson/silverstripe-zenvalidator

Faster, easier client and server-side form validation for SilverStripe

5877.4k2](/packages/sheadawson-silverstripe-zenvalidator)[timehunter/laravel-google-recaptcha-v2

Laravel Package for google reCAPTCHA v2

1393.0k](/packages/timehunter-laravel-google-recaptcha-v2)[aryehraber/statamic-captcha

Protect your Statamic forms using a Captcha service

17189.0k](/packages/aryehraber-statamic-captcha)[palmtree/form

Form builder with Bootstrap v5/v4 classes, validation, Recaptcha support, AJAX submissions and more

384.6k1](/packages/palmtree-form)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
