// === Google reCAPTCHA Integration for WooCommerce Registration === // Define your reCAPTCHA Site and Secret Keys define('RECAPTCHA_SITE_KEY', '6Lf9nzIrAAAAAEUPLXX63hk8V1lXNhWXaGijS1l-'); define('RECAPTCHA_SECRET_KEY', '6Lf9nzIrAAAAANxihbX9U41YwDB41g6KA5gHLZ3C'); // 1. Display reCAPTCHA on the WooCommerce registration form add_action('woocommerce_register_form', 'add_recaptcha_to_registration_form', 15); function add_recaptcha_to_registration_form() { echo '
'; } // 2. Load the Google reCAPTCHA script add_action('wp_enqueue_scripts', 'load_recaptcha_script'); function load_recaptcha_script() { wp_enqueue_script('google-recaptcha', 'https://www.google.com/recaptcha/api.js', array(), null, true); } // 3. Validate the reCAPTCHA response on registration form submission add_action('woocommerce_register_post', 'validate_recaptcha_response', 10, 3); function validate_recaptcha_response($username, $email, $validation_errors) { if (empty($_POST['g-recaptcha-response'])) { $validation_errors->add('captcha_error', __('Please complete the reCAPTCHA.', 'woocommerce')); } else { $response = sanitize_text_field($_POST['g-recaptcha-response']); $remote_ip = $_SERVER['REMOTE_ADDR']; $verify = wp_remote_get("https://www.google.com/recaptcha/api/siteverify?secret=" . RECAPTCHA_SECRET_KEY . "&response={$response}&remoteip={$remote_ip}"); $response_body = wp_remote_retrieve_body($verify); $result = json_decode($response_body, true); if (empty($result['success'])) { $validation_errors->add('captcha_error', __('reCAPTCHA verification failed. Please try again.', 'woocommerce')); } } }