Home / Admin / Conditionally disable Cloudflare Turnstile
Duplicate Snippet

Embed Snippet on Your Site

Conditionally disable Cloudflare Turnstile

This code snippet turns off the CAPTCHA setting in form data so Turnstile is not initialized on the form.

<10
Code Preview
php
<?php
/**
 * Conditionally disable Cloudflare Turnstile
 *
 * @param array $form_data Form data.
 * @return array Modified form data.
 */
function wpf_disable_turnstile( $form_data ) {
    // Disable CAPTCHA setting.
    $form_data['settings']['recaptcha'] = '0';
    return $form_data;
}
add_filter( 'wpforms_frontend_form_data', 'wpf_disable_turnstile', 10, 1 );
add_filter( 'wpforms_process_before_form_data', 'wpf_disable_turnstile', 10, 1 );
/**
 * Additional runtime bypass for Cloudflare Turnstile.
 */
add_action( 'init', static function() {
    // Bypass backend CAPTCHA validation as a fallback.
    add_filter( 'wpforms_process_bypass_captcha', '__return_true' );
    // Prevent Turnstile API script from loading.
    add_filter( 'wpforms_frontend_captcha_api', function( $captcha_api ) {
        $captcha_settings = wpforms_get_captcha_settings();
        if ( $captcha_settings['provider'] === 'turnstile' ) {
            return '';
        }
        return $captcha_api;
    }, 10, 1 );
}, 11 );

Comments

Add a Comment