Google ReCAPTCHA with AJAX

Google ReCAPTCHA with AJAX

This is how you can pass on AJAX .js

var userdata = { 
  'action'   : 'signin_member', 
  'username' : jQuery('#user_name').val(),
  'password' : jQuery('#password').val(),
  'hiddenurl': jQuery('#hiddenurl').val(),
  'captcha'  : grecaptcha.getResponse(0)
 };

This is how you can handle the captcha ajax response

/*Validate Coupon Codes*/

add_action( 'wp_ajax_validate_scratchcode', 'validate_scratchcode_callback' ); 
add_action( 'wp_ajax_nopriv_validate_scratchcode', 'validate_scratchcode_callback' ); 
function validate_scratchcode_callback(){

	$secret		=	"6LfYNNIZAAAAAA_eGXqymAFPe8yxQV5TITMUykhs";
	$response	=	$_POST["captcha"];
	$verify		=	file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret={$secret}&response={$response}");
	$captcha_success=json_decode($verify);


	if($captcha_success->success==true){

	}

	}else{
		 echo json_encode(array('invalidcaptcha'=>true, 'redirect'=> ''));
	}

wp_die();
}
/*Validate Coupon Codes*/

The HTML Looks like


Leave a Reply

Your email address will not be published. Required fields are marked *