Integrate Easy Captcha with woocommerce checkout
If you ever faced the problem of integrating a captcha on a woocommerce wordpress site this can be an easy solution for you.
Using Easy Captcha plugin you can connect your captcha to the correct hook to limit spam on your ecommerce.
Since easy captcha seems not overridable we will modify it directly.
The procedure is quite simple:
- Edit easy-captcha.php in the plugin directory, around line 234 you will find "easy_captcha_get_avalable_pages" function, here we will add a new value to pages array, for example 'form_billing':
function easy_captcha_get_avalable_pages() { $pages = array(); $pages['login_page'] = __('Login page'); $pages['registration_page'] = __('Registration page'); $pages['comments_form'] = __('Comments form'); $pages['password_reset_page'] = __('Password reset page'); $pages['form_billing'] = __('Checkout billing page'); return $pages; }
- Next we will create a new file containing our hooks.
The file should be named "easy_captcha_[name you choose in the previous step].php" for example "easy_captcha_form_billing.php".
Inside this file we will write our custom function.I'll connect the captcha to the register form inside billing page, you can connect it to the billing data replacing "woocommerce_after_checkout_registration_form" with "woocommerce_after_checkout_billing_form".Be sure to replace every occurrence of "form_billing" with the name you choose previously!
<?php function easy_captcha_install_captcha_form_billing() { add_action('woocommerce_after_checkout_registration_form', 'easy_captcha_show_captcha_form_billing'); add_filter('woocommerce_after_checkout_validation', 'easy_captcha_validate_captcha_form_billing'); } function easy_captcha_show_captcha_form_billing() { $captcha = easy_captcha_get_setting("easy_captcha-form_billing", 'hidden'); require_once "easy-captcha-{$captcha}.php"; $fn = "easy_captcha_show_captcha_{$captcha}"; $fn('form_billing'); return true; } function easy_captcha_validate_captcha_form_billing($results) { global $woocommerce; $captcha = easy_captcha_get_setting("easy_captcha-form_billing", 'hidden'); require_once "easy-captcha-{$captcha}.php"; $fn = "easy_captcha_check_captcha_{$captcha}"; $result = $fn('form_billing'); $result = easy_captcha_get_captcha_result($result, __('Captcha failed!')); if ($result !== TRUE) { $woocommerce->add_error('Captcha ' . $result); } }
- Next we can edit what king of captcha to display directly from plugin setting page in admin area.
You should have now a captha enabled in you checkout process.
If you want to enable the same captcha in login page or registration page of woocommerce (you can enable registration page near login page from woocommerce settings), just add the call to the action you like, for example "do_action('login_form')" and "do_action('register_form')" in "form-login.php".
Remember that you can replace woocommerce template file by creating a woocommerce folder in your theme folder and adding directly inside of it a folder named like the one you will find in template folder inside woocommerce plugin diretcory, for example: "wp-content/themes/twentyeleven/woocommerce/myaccount/form-login.php".
Have fun!