Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**reCAPTCHA*/
- function ava_custom_recaptcha(){
- ?>
- <script type="text/javascript">
- var key = 'PUBLIC KEY HERE';
- var form = jQuery(".avia_ajax_form");
- var button = jQuery(form).find(".button");
- var parent = button.parent(".form_element");
- var captcha = jQuery("<p data-sitekey='"+ key +"' id='reCAPTCHAV3'></p>");
- var answer = jQuery("<p class='answer'>You have to pass reCAPTCHA before you can send the form.</p>");
- var publickey = captcha.data('sitekey');
- var createCaptcha = function() {
- button.attr("disabled", "disabled");
- button.css("display", 'none');
- form.attr('action', '');
- captcha.insertBefore(parent);
- answer.insertAfter(parent);
- };
- createCaptcha();
- grecaptcha.ready(function() {
- grecaptcha.execute(key, {action: 'homepage'}).then(function(token) {
- console.log(token);
- setTimeout( function() {
- onVerifyCallback(response);
- }, 100 );
- });
- });
- var onVerifyCallback = function(captcharesp) {
- jQuery.ajax({
- type: "POST",
- url: avia_framework_globals.ajaxurl,
- data: {
- recaptcha: captcharesp,
- action: 'avia_ajax_recaptcha'
- },
- success: function(response) {
- console.log('success', response);
- if(response == 'success') {
- form.attr('action', window.location.href.replace("#", ""));
- button.removeAttr('disabled');
- button.css("display", 'block');
- jQuery('.answer').remove();
- }
- },
- error: function() {
- console.log('error');
- },
- complete: function() {
- console.log('complete');
- }
- });
- }
- </script>
- <?php
- }
- add_action('wp_footer', 'ava_custom_recaptcha');
- add_action( 'wp_ajax_avia_ajax_recaptcha', 'avia_ajax_recaptcha' );
- add_action( 'wp_ajax_nopriv_avia_ajax_recaptcha', 'avia_ajax_recaptcha' );
- function avia_ajax_recaptcha()
- {
- $gcaptcha = $_POST['recaptcha'];
- if(isset($gcaptcha)):
- $secret = '6LeB_HoUAAAAAH5gmXvzfIiGGK5TMSTR-eqVaYTM';
- $response = json_decode(file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$secret."&response=".$gcaptcha."&remoteip=".$_SERVER['REMOTE_ADDR']), true);
- if($response['success']):
- echo 'success';
- else:
- echo 'error';
- endif;
- else:
- echo 'invalid captcha';
- endif;
- die();
- }
- if( ! function_exists( 'avia_register_reCAPTCHAV2_scripts' ) )
- {
- if( ! is_admin() ){
- add_action( 'wp_enqueue_scripts', 'avia_register_reCAPTCHAV2_scripts' );
- }
- function avia_register_reCAPTCHAV2_scripts()
- {
- $key = 'PUBLIC KEY HERE';
- $api_url = 'https://www.google.com/recaptcha/api.js?render='.$key;
- wp_register_script( 'avia-google-reCAPTCHAV2-api', $api_url, false, '1.0.0', false);
- wp_enqueue_script( 'avia-google-reCAPTCHAV2-api' );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement