Tagged: , ,

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #1235377

    While playing around with the new version of my website, I came up with an Idea for the recaptcha script used by the widely spreaded CF7 plugin. Use the following snipet to load recaptcha only if the visitor has given his permission to do so.

    // recaptcha only if consent cookie
    function ss_wpcf7_recaptcha_enqueue_scripts() {
        
        $googlerecaptchacookie = 'aviaPrivacyGoogleReCaptchaDisabled';
        
        $enable_recaptcha = false;
        
        if (array_key_exists($googlerecaptchacookie, $_COOKIE) && isset($_COOKIE[$googlerecaptchacookie])) {
            $disable_recaptcha_check = boolval ($_COOKIE[$googlerecaptchacookie]);
             $enable_recaptcha = $disable_recaptcha_check;
        } else {
            $enable_recaptcha = true;
        }
        
        if($enable_recaptcha):
            
            $service = WPCF7_RECAPTCHA::get_instance();
    
            if ( ! $service->is_active() ) {
                return;
            }
    
            wp_enqueue_script( 'google-recaptcha',
                add_query_arg(
                    array(
                        'render' => $service->get_sitekey(),
                    ),
                    'https://www.google.com/recaptcha/api.js'
                ),
                array(),
                '3.0',
                true
            );
    
            wp_enqueue_script( 'wpcf7-recaptcha',
                wpcf7_plugin_url( 'modules/recaptcha/script.js' ),
                array( 'google-recaptcha' ),
                WPCF7_VERSION,
                true
            );
    
            wp_localize_script( 'wpcf7-recaptcha',
                'wpcf7_recaptcha',
                array(
                    'sitekey' => $service->get_sitekey(),
                    'actions' => apply_filters( 'wpcf7_recaptcha_actions', array(
                        'homepage' => 'homepage',
                        'contactform' => 'contactform',
                    ) ),
                )
            );
        
        endif;
    }
    remove_action( 'wp_enqueue_scripts', 'wpcf7_recaptcha_enqueue_scripts', 10, 0 );
    add_action( 'wp_enqueue_scripts', 'ss_wpcf7_recaptcha_enqueue_scripts', 10, 0 );

    Maybe this is useful for someone, so I share that here.

    #1235644

    Hey André,

    Thanks a lot for sharing, it’s much appreciated :-)

    Best regards,
    Rikard

    #1235749

    is there anything to adjust in your code – that it works?
    Where did you enter the api key on cf7 integration – or on Enfold Options?

    Thanks

    #1235786

    The api key has to be entered into the CF7 plugin settings, submenu integrations. The code is basically copied from the original CF7 plugin, wrapped with a little check and then readded. Works like a charm on my site.

    #1235811

    Nice – thanks for the code.
    I prefer my borlabs cookie – but this is a good addition for enfold usage.

Viewing 5 posts - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.