friendly-captcha-wordpress icon indicating copy to clipboard operation
friendly-captcha-wordpress copied to clipboard

Script is requested twice when used with Gravity Forms

Open mmatiasautio opened this issue 6 months ago • 1 comments

Hi,

I have an issue where the script on the page (widget.module.min.js for V1, the same issue is with V2 though), is requested twice on the page. This causes the loading of the widget to fail due to a CORS error. The two requests are otherwise identical but the other one is missing the access-control-allow-origin header. And well, there is no reason to load the script twice.

I then found this function . If I force the plugin to load the script using the frcaptcha_v1_echo_script_tags(), it loads correctly. So maybe this function should be used with Gravity Forms as well, and not just WPForms. Or try to find the underlying issue.

mmatiasautio avatar May 08 '25 07:05 mmatiasautio

function frcaptcha_enqueue_widget_scripts( $forceMutationObserver = false ) {
    $plugin = FriendlyCaptcha_Plugin::$instance;

    if ( ! $plugin->is_configured() ) {
        return;
    }

    if( \is_admin() ) {
        if ( $plugin->get_enable_v2() ) {
            frcaptcha_v2_enqueue_widget_scripts();
        } else {
            frcaptcha_v1_enqueue_widget_scripts();
        }
    } else {
        if ( $plugin->get_enable_v2() ) {
            frcaptcha_v2_echo_script_tags();
        } else {
            frcaptcha_v1_echo_script_tags();
        }
    }

    frcaptcha_mutation_observer_scripts( $plugin, $forceMutationObserver );
}

So this is how I got it to work both in admin and front.

mmatiasautio avatar May 08 '25 13:05 mmatiasautio

Thanks for reporting this issue!

I would prefer to use wp_enqueue_script whenever possible because that's the recommended way to add scripts in WordPress. Reading through the notes in the wp_enqueue_script documentation it mentions that this function should only be used in the wp_enqueue_scripts or admin_enqueue_scripts action hooks. We don't do that right now, so maybe that's the cause of the problem ...

merlinfuchs avatar Jul 01 '25 09:07 merlinfuchs

@mmatiasautio Could you checkout the gf-proper-enqueue branch to see if it resolves your issue? It uses the wp_enqueue_scripts to enqueue the script. Thanks!

merlinfuchs avatar Jul 01 '25 09:07 merlinfuchs

Closing this in favor of #159 because I can't reproduce this particular issue, but I suspect it's related. Feel free to re-open if you have more info!

merlinfuchs avatar Jul 08 '25 07:07 merlinfuchs

Hi! Sorry for the delay, but I tested the fix and unfortunately it doesn't solve the issue... I could send you a link privately to the staging environment if you want to have a look. @merlinfuchs

mmatiasautio avatar Sep 04 '25 07:09 mmatiasautio