• Resolved ctrlaltdelete

    (@ctrlaltdelete)


    I want to protect my jquery button from bots without annoying the users, so i thought of adding google’s invisible recaptcha to it. However implementation isn’t as easy as i though and i can’t seem to do it. Any help appreciated. PS: I’m not a coder.

    And this is what i have:

    HTML:

    <button class="acf-get-content-button">Show Link</button>
    <div class="fa" id="acf-content-wrapper" data-id="<?php echo $post_id; ?>"></div>

    JS:

    <script>
    (function($) {
      $('.acf-get-content-button').click(function(e) {
        e.preventDefault();
        $('.fa').addClass('fa-cog fa-spin fa-4x');
        var $contentWrapper = $('#acf-content-wrapper');
        var postId = $contentWrapper.data('id');
    
        $.ajax({
            url: "/public/ajax.php",
            type: "POST",
            data: {
              'post_id': postId
            },
          })
          .done(function(data) {
            $('.fa').removeClass('fa-cog fa-spin fa-4x');
            $contentWrapper.append(data);
            $('.acf-get-content-button').removeClass().addClass('.acf-get-content-button')
          });
      });
      $('.acf-get-content-button').mouseup(function() {
        if (event.which == 1) {
          $(".acf-get-content-button").hide();
        }
      });
    })(jQuery);
    </script>

    ajax.php

    <?php
    define('WP_USE_THEMES', false);
    require_once( $_SERVER['DOCUMENT_ROOT'] . '/wp-load.php' );
    global $post;
    $post_id = $_REQUEST["post_id"];
    $content = get_field( 'ebook_link_pdf', $post_id );
    echo ($content);
Viewing 8 replies - 1 through 8 (of 8 total)
  • Plugin Author MihChe

    (@mihche)

    Hi,
    You have to render the invisible recaptcha widget inside the form. Has nothing to do with the button.

    Add <?php do_action('google_invre_render_widget_action'); ?> before form element closing tag

    ~Mihai

    Thread Starter ctrlaltdelete

    (@ctrlaltdelete)

    Hi i don’t have form just the button. Should i change it to:

    <form>
    <button class="acf-get-content-button">Show Link</button>
    <div class="fa" id="acf-content-wrapper" data-id="<?php echo $post_id; ?>"></div>
    <?php do_action('google_invre_render_widget_action'); ?>
    </form>

    Is that all there is to it? Thanks!

    Thread Starter ctrlaltdelete

    (@ctrlaltdelete)

    Sorry, was my last post the correct implementation? How can i test its working properly?

    Plugin Author MihChe

    (@mihche)

    Hi,
    yes it seems to be correct. Now you have to validate the request. On the servers side, where the request is validated check the recaptcha response:

    $is_valid = apply_filters('google_invre_is_valid_request_filter', true);
    if( ! $is_valid )
    {
        // handle error here
    }
    else
    {
        // continue with your logic
    }
    Thread Starter ctrlaltdelete

    (@ctrlaltdelete)

    Good so something like this would do? Sorry just making sure.

    ajax.php

    <?php
    define('WP_USE_THEMES', false);
    require_once( $_SERVER['DOCUMENT_ROOT'] . '/wp-load.php' );
    global $post;
    $post_id = $_REQUEST["post_id"];
    $content = get_field( 'ebook_link_pdf', $post_id );
    
    $is_valid = apply_filters('google_invre_is_valid_request_filter', true);
    if( ! $is_valid )
    {
        echo 'wrong captcha resolution';
    }
    else
    {
        // continue with your logic
        echo ($content);
    }

    Oh and if it is correct how do i confirm it’s actually challenging bots? since it’s invisible.

    • This reply was modified 7 years, 9 months ago by ctrlaltdelete.
    Plugin Author MihChe

    (@mihche)

    Hi @ctrlaltdelete,
    No, that code should not be there! In order to help you with this I need to see the entire code. Please contact me through https://www.wpbruiser.com/contact/

    ~Mihai

    Thread Starter ctrlaltdelete

    (@ctrlaltdelete)

    Message sent!

    Is this already implemented out-of-the-box for Gravity Forms AJAX forms / user registrations? I know for the standard reCAPTCHA their documentation specifies not to use AJAX due to limitations from (regular) reCAPTCHA..

    – SRS

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Possible to add it to ajax button?’ is closed to new replies.