I think i over complicated a bit 🙂
using this hook woocommerce_register_post
would simply things.
This is my content of the custom-sum-captcha.php file:
function custom_sum_captcha_generate_html() {
$num1 = rand(1, 10);
$num2 = rand(1, 10);
$sum = $num1 + $num2;
$html="<p>Please solve the following math problem:</p>";
$html .= '<p>' . $num1 . ' + ' . $num2 . ' = <input type="text" id="captcha-answer" name="captcha-answer" style="width:20%"/></p>';
$html .= '<input type="hidden" id="captcha-sum" name="captcha-sum" value="' . $sum . '" />';
return $html;
}
// Shortcode handler
function custom_sum_captcha_shortcode() {
return custom_sum_captcha_generate_html();
}
add_shortcode('custom_sum_captcha', 'custom_sum_captcha_shortcode');
// Validate CAPTCHA on registration form submission
function custom_sum_captcha_validate_registration_form($username, $email, $validation_errors) {
// Check if the CAPTCHA answer is provided
if (empty($_POST['captcha-answer'])) {
$validation_errors->add('captcha_error', __('Please enter the CAPTCHA answer.', 'textdomain'));
} else {
// Validate the CAPTCHA answer
$captcha_answer = intval($_POST['captcha-answer']);
$real_answer = intval($_POST['captcha-sum']);
if ($captcha_answer !== $real_answer) {
$validation_errors->add('captcha_error', __('The CAPTCHA answer is incorrect.'.$captcha_answer.'---'.$real_answer, 'textdomain'));
}
}
}
add_action('woocommerce_register_post', 'custom_sum_captcha_validate_registration_form', 10, 3);
And i have added <?php echo custom_sum_captcha_generate_html(); ?>
in the form-login.php
file
and that’s it ! no more js , no more ajax