friendly-captcha-wordpress
friendly-captcha-wordpress copied to clipboard
Script is requested twice when used with Gravity Forms
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.
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.
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 ...
@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!
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!
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