How to translate reCAPTCHA in Contact Form 7

Recently I implemented Contact Form 7 form on one of my client’s website. Contact Form 7 allows you to add Google reCAPTCHA to your form to help protecting your website and email from SPAM which is great. However, there was one problem. My client’s website is not in English and implemented reCAPTCHA didn’t mirror the language set in WordPress. If you can edit your WordPress theme or child theme, you can set a language for reCAPTCHA in Contact Form 7 by adding the language code to reCAPTCHA JavaScript url.

reCAPTCHA in Contact Form 7 before and after

How to translate reCAPTCHA in Contact Form 7

In your custom WordPress theme or child theme (you should use a child theme if you use any other than custom theme, otherwise your changes will be deleted next time the parent theme is updated) locate a file called functions.php or any other functions template that’s included in functions.php, usually located in inc folder. Look for something called template-tags.php or extras.php.

Copy and paste the following code at the end of functions.php.

/**
 * Set language for reCaptcha in Contact Form 7
 */
remove_action( 'wpcf7_enqueue_scripts', 'wpcf7_recaptcha_enqueue_scripts' );
add_action( 'wpcf7_enqueue_scripts', 'mytheme_wpcf7_recaptcha_enqueue_scripts' );

function mytheme_wpcf7_recaptcha_enqueue_scripts() {
    $hl = get_locale();

    $url = 'https://www.google.com/recaptcha/api.js';
    $url = add_query_arg( array(
        'hl' => $hl,
        'onload' => 'recaptchaCallback',
        'render' => 'explicit' ), $url );

    wp_register_script( 'google-recaptcha', $url, array(), '2.0', true );
}

In two places the code says mytheme. Replace it with a prefix that’s used in your theme. You can have a look round the functions.php file and most likely you will see that many functions start with a certain prefix similar to the name of your theme. Use that one. Just make sure you don’t add any extra spaces and that the two function names mytheme_wpcf7_recaptcha_enqueue_scripts are identical. If you don’t use your own prefix, it will still work but it’s a good practice to use your own unique one in case some other plugin used a function with the same name.

Save the edited file. Your reCAPTCHA in Contact Form 7 will now translate to the language that is set in your WordPress settings.

[signature]

[sign-up]

Comments

  1. This works. Thank you very much.

    • You’re welcome.

  2. Thx!!

Leave a Reply

Your email address will not be published. Required fields are marked *