PHP code example of pollen-solutions / recaptcha

1. Go to this page and download the library: Download pollen-solutions/recaptcha library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

pollen-solutions / recaptcha example snippets


use Laminas\HttpHandlerRunner\Emitter\SapiEmitter;
use Pollen\Form\FormManager;
use Pollen\Recaptcha\Recaptcha;
use Pollen\Recaptcha\Form\RecaptchaFormField;

$recaptcha = new Recaptcha();
$recaptcha->config(
    [
        /**
         * Recaptcha version or type ().
         * @var string|null $secretkey
         */
        'secretkey' => '===== secretkey =====',
        /**
         * Locale in ISO 15897 format. en_US if null.
         * @var string|null $locale
         */
        'locale'    => null,
    ]
);

$forms = new FormManager();

try {
    $forms->registerFormFieldDriver('recaptcha', new RecaptchaFormField($recaptcha));
} catch(Throwable $e) {
    // If the recaptcha form field is already registered.
    unset($e);
}

$form = $forms->buildForm(
    [
        'fields' => [
            'email'   => [
                'type' => 'text',
            ],
            'captcha' => [
                'type' => 'recaptcha',
            ],
        ],
    ]
)->get();

if ($response = $form->handle()->proceed()) {
    (new SapiEmitter())->emit($response->psr());
    exit;
}

echo <<< HTML
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Test Recaptcha</title>
    </head>
    <body>
    $form
HTML;

// A Recaptcha widget was then automatically added by the form render.
$jsScripts = $recaptcha->getJsScripts();

echo <<< HTML
    <!-- Recaptcha Scripts -->
    <script type="text/javascript">/* <![CDATA[ */$jsScripts/* ]]> */</script>
    <!-- / Recaptcha Scripts -->
    </body>
    </html>
HTML;

use Pollen\Recaptcha\RecaptchaInstance;

/** @var RecaptchaInstance $recaptcha */
$recaptcha = $this->config(['asset.autoloader' => true]);

add_action('wp_print_footer_scripts', function () use ($recaptcha) {
    echo "<!-- Recaptcha Scripts -->" .
    "<script type=\"text/javascript\">/* <![CDATA[ */" . $recaptcha->getJsScripts() . "/* ]]> */</script>" .
    "<!-- / Recaptcha Scripts -->";
});