Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
[Forminator Pro] - Fix Google's ReCaptcha conflict with Divi Theme
<?php
/**
* Plugin Name: [Forminator Pro] - Fix Google's ReCaptcha conflict with Divi Theme
* Plugin URI: https://premium.wpmudev.org/
* Description: Dequeue Divi's ReCaptcha scripts when Forminator is activated to avoid JS conflicts (as of 1.12)
* Author: Alessandro Kaounas @ WPMUDEV
* Author URI: https://premium.wpmudev.org/
* Task: 0/1135022585412927/1166085706874201
* License: GPLv2 or later
*/
add_action( 'template_redirect', function(){
if( ! class_exists( 'Forminator_Recaptcha' ) ){
return;
}
// Remove Divi's ReCaptcha scripts
remove_action( 'wp_enqueue_scripts', array( ET_Core_API_Spam_Providers::instance()->get( 'recaptcha', '' ), 'action_wp_enqueue_scripts' ) );
});
@stracker-phil

This comment has been minimized.

Copy link

@stracker-phil stracker-phil commented Dec 2, 2020

This code solves the problem in a different way:

It removes Forminators recaptcha code and injects it with a script-loader. The ReCaptcha API of Forminator is loaded after the page finished loading. This has two side effects:

  1. Forminator will not overwrite Divis recaptcha settings.
  2. ReCaptcha is loaded with a delay, which improves page performance.
add_action( 'wp_footer', function() {
    if ( ! wp_script_is( 'forminator-google-recaptcha', 'enqueued' ) ) {
        return;
    }

    // De-queue Forminators ReCaptcha.
    wp_dequeue_script( 'forminator-google-recaptcha' );

    // Inject a script-loader script that loads Forminators
    printf(
        '<script>!function(d,t,s,e,a){e=d.createElement(t);a=d.getElementsByTagName(t)[0];e.async=!0;e.src=s;a.parentNode.insertBefore(e,a)}(document,"script","%s")</script>',
        esc_attr( $GLOBALS['wp_scripts']->registered['forminator-google-recaptcha']->src )
    );
}
}, 10);

Here's the un-minified code of that script-loader, for the records:

function load_script(scriptUrl) {
    var element = document.createElement("script");
    var firstElement = document.getElementsByTagName("script")[0];
    element.async = true;
    element.src = scriptUrl;
    firstElement.parentNode.insertBefore(element, firstElement);
}
load_script( "forminator url here" );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment