Skip to main content

Protect any form

Two steps: show the placeholder inside the form, and check the request in PHP.

This code will cause a fatal error if the Invisible Recaptcha plugin is not active;

optionally, add a check for class_exists('InvisibleReCaptcha') and handle the case where the plugin is not available. :::

1. Add the placeholder inside the <form> tag, before the closing tag:

<form method="post">
<!-- your fields -->
<?php \InvisibleReCaptcha\Modules\WordPress\WordPressPublicModule::getInstance()->renderReCaptchaHolderHtmlCode(); ?>
<button type="submit">Send</button>
</form>

2. Validate the request where you process the form:

$is_valid = \InvisibleReCaptcha\MchLib\Modules\MchBasePublicModule::isRecaptchaValid();

if ( ! $is_valid ) {
// Stop here. Show an error or redirect.
return;
}

// Continue with your logic.

Good to know​

  • The form needs the reCAPTCHA placeholder inside the <form> tag.
  • The Site Key and Secret Key must be saved in Settings. Without keys, every request is invalid.
  • The validation function returns false if the token is missing, and otherwise asks Google. The result is cached for the rest of the request.
  • After the check, the form is sent as a normal browser request. For AJAX forms, give the submit button the class invisible-recaptcha-do-submit. See Form won't submit.
  • If a page has nested forms, the placeholder belongs to the form it is directly inside.