Home / Admin / Set a specific language for Cloudflare Turnstile.
Duplicate Snippet

Embed Snippet on Your Site

Set a specific language for Cloudflare Turnstile.

<10
Code Preview
js
/**
 * Set a specific language for Cloudflare Turnstile.
 *
 * @link https://wpforms.com/developers/how-to-set-the-language-for-cloudflare-turnstile
 */
function wpf_add_turnstile_language_script() {
    if (!wp_script_is('wpforms-recaptcha', 'enqueued')) {
        return;
    }
    $language_code = 'ar-eg'; // where 'ar-eg' is for Arabic (Egypt) language
    
    ?>
    <script type="text/javascript">
    document.addEventListener('DOMContentLoaded', function() {
        if (typeof turnstile === 'undefined') return;
        // Override turnstile's render function to set the language
        var originalRender = turnstile.render;
        turnstile.render = function(container, options) {
            options = Object.assign({ language: '<?php echo esc_js($language_code); ?>' }, options || {});
            return originalRender(container, options);
        };
        // Reinitialize Turnstile when WPForms reCAPTCHA is loaded
        document.addEventListener('wpformsRecaptchaLoaded', function() {
            document.querySelectorAll('.wpforms-captcha').forEach(function(el) {
                if (el.closest('[class*="wpforms-is-turnstile"]')) {
                    turnstile.remove(el.getAttribute('data-captcha-id'));
                    turnstile.render(el, {
                        sitekey: el.getAttribute('data-sitekey'),
                        language: '<?php echo esc_js($language_code); ?>'
                    });
                }
            });
        });
    });
    </script>
    <?php
}
add_action('wpforms_wp_footer_end', 'wpf_add_turnstile_language_script', 100);

Comments

Add a Comment