Skip to content

Instantly share code, notes, and snippets.

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

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' ) ) {

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

    // Inject a script-loader script that loads Forminators
        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