-
-
Save tharlab/ac29d106be27a53d4176e7caa26afc54 to your computer and use it in GitHub Desktop.
Add reCaptcha v2 to WooCommerce Checkout
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Add reCaptcha to checkout form | |
* Note: Can't place within the payment part of the form, WooCommerce just won't show it, choose an appropriate action to add it to accordingly | |
* @param $checkout | |
*/ | |
function doublee_show_me_the_checkout_captcha($checkout) { | |
echo '<div class="g-recaptcha" data-sitekey="YOUR_KEY_HERE"></div>'; | |
} | |
add_action('woocommerce_checkout_order_review', 'doublee_show_me_the_checkout_captcha', 18); | |
/** | |
* Validate reCaptcha | |
*/ | |
function doublee_process_recaptcha() { | |
$postdata = $_POST['g-recaptcha-response']; | |
$verified_recaptcha = file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret=YOUR_KEY_HERE&response='.$postdata); | |
$response = json_decode($verified_recaptcha); | |
if(!$response->success) { | |
wc_add_notice('Please verify that you are not a robot' ,'error'); | |
} | |
} | |
add_action('woocommerce_checkout_process', 'doublee_process_recaptcha'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
wp_enqueue_script('recaptcha', '//www.google.com/recaptcha/api.js', '', '', true); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment