Question:

I was asked to modify a website so that it complied with privacy guidelines as much as possible. This meant that the reCAPTCHA service could only be loaded by Contact Form 7 once permission had been given to place cookies.
Complianz was used to determine whether or not cookies could be placed. This plugin recognized the reCAPTCHA service but did not block it. As a result, fonts, images, etc. for reCAPTCHA were always loaded. This meant that the IP addresses of visitors were shared with Google, which was of course not correct as long as no permission had been given.
Contact Form 7 also does not offer an option to easily enable or disable reCAPTCHA based on cookies or for a specific form. It is either the service on or off.
Answer:
I have written and tested the PHP script below:
add_action( 'init', function() {
if ( function_exists('cmplz_has_consent') && ! cmplz_has_consent('marketing') ) {
remove_action( 'wpcf7_init', 'wpcf7_recaptcha_register_service', 40);
add_action( 'wp_enqueue_scripts', function() {
remove_action( 'wp_enqueue_scripts', 'wpcf7_recaptcha_enqueue_scripts', 20 );
}, 1 );
remove_filter( 'wpcf7_form_hidden_fields', 'wpcf7_recaptcha_add_hidden_fields', 100 );
add_filter( 'wpcf7_skip_spam_check', '__return_true', 10 );
}
}, 50);
You can copy paste it into a own plugin or the functions file of your (child) theme.
The first if statement will check if the function cmplz_has_consent() exists and that consent is not given for the category marketing. In Complianz the category markering is for cookies that are set by 3rd party companies like Google or Facebook or Instagram or another company.
So if no consent is given the PHP code will:
- Not load the scripts needed to load the reCAPTCHA service
- Remove the hidden fields in the form to execute the reCAPTCHA checks
- Removed the spam check that will always fail because of 1 and 2 above
Offcourse you can change the PHP script easily for your own situation. You can add an extra if statement to only execute it for a specific page or Contact Form 7 form.
> Read also my Dutch blog about my Akismet integration for Elementor
Tested on:
WordPress 6.8.2 with Contact Form 7.1.1
