In this guide, learn how to use reCAPTCHA with your Form Builder forms in Breakdance. This process involves selecting your form builder element, configuring reCAPTCHA settings, and obtaining API keys from the Google Cloud Console.
After saving your settings in the form builder, you can verify the activation of reCAPTCHA by viewing the form on the frontend. Look for the reCAPTCHA badge near the bottom right of the page, indicating that reCAPTCHA is active.
By default, Breakdance loads reCAPTCHA via wp_enqueue_script. However, it is possible to lazy-load reCAPTCHA with some custom code.
<?php
// Disable Breakdance reCAPTCHA enqueue
add_filter('breakdance_load_recaptcha_script', '__return_false');
// Load reCAPTCHA script on first scroll
add_action('wp_footer', function() {
$apiKey = \Breakdance\APIKeys\getKey('recaptcha_site_key'); // The API key defined in Breakdance settings
?>
<script>
(function() {
let loaded = false;
function loadRecaptcha() {
if (loaded) return;
loaded = true;
let s = document.createElement('script');
s.src = 'https://www.google.com/recaptcha/api.js?render=<?php echo esc_js($apiKey); ?>';
s.async = true;
document.body.appendChild(s);
}
const events = ['scroll', 'mousemove', 'touchstart'];
events.forEach(event => {
window.addEventListener(event, loadRecaptcha, { once: true, passive: true });
});
})();
</script>
<?php
});