mary eltresse blog
Mary Eltresse - Adding label to WP Forms Captcha

How to add label to WP Forms Google Recaptcha(V2)

When optimizing our website, we encountered a common accessibility issue:

“Form elements do not have associated labels”

Specifically, this pertains to the Google ReCaptcha field on forms built with WPForms.

Accessibility tools flag this because the ReCaptcha widget does not have an associated label, making it less user-friendly for screen readers.

mary eltresse fixing form elements do not have associated labels

The Problem

Although WPForms provides extensive hooks and filters for customization, there isn’t a built-in hook for adding a label to the ReCaptcha field.

This lack of a predefined solution means a developer must delve into the plugin’s codebase to implement the solution.

This is where the Google captcha is generated in WP Forms plugin.
This code is located in src > Frontend > Captcha.php inside the wpforms-lite folder.
This method is called inside the recaptcha method of the Captcha class.
The recaptcha method is used as a callback in the wpforms_frontend_output action hook.
This hook is what we used to add the label to the captcha field.

The Solution

After analyzing the WPForms code, we created a custom function to add an accessible label to the ReCaptcha field.

To make the ReCaptcha field accessible and compliant with accessibility standards, we created a custom callback function that injects an accessible label and dynamically associates it with the ReCaptcha container.

Here’s the full implementation:

The get_form_captcha_settings function checks if ReCaptcha is properly configured for the form and retrieves the settings.

This is adapted from WPForms’ private methods to ensure compatibility.

Key Features of the Solution

Accessible Label

The code adds a visually hidden but screen-reader-detectable label (screen-reader-text) for the ReCaptcha field. It ensures screen readers read: “Please complete the CAPTCHA to verify you are human.”

Dynamic Association

The aria-labelledby attribute is dynamically assigned to the ReCaptcha container. This ensures accessibility tools like Lighthouse detect the association and validate it successfully.

Use of data-no-defer="1"

Adding data-no-defer="1" to the <script> tag ensures:

  • Compatibility with LiteSpeed Cache: LiteSpeed optimizations often defer scripts, which might cause issues if ReCaptcha’s dynamic association isn’t processed in time.
  • Lighthouse Detection: Ensures the script runs without deferring, so Lighthouse can detect the aria-labelledby attribute association properly.

Improved Lighthouse Scores

With the script executed without deferring, the aria-labelledby attribute is accurately detected during Lighthouse audits, addressing the “Form elements do not have associated labels” warning.

Why Use data-no-defer="1?

LiteSpeed Cache and other performance tools often defer scripts to improve loading speeds.

However, accessibility-related scripts like this one need to run immediately after the DOM is loaded to ensure elements are correctly labeled.

The data-no-defer="1" attribute ensures the script is not deferred while retaining compatibility with LiteSpeed.

Steps to Implement

  1. Add the Code: Copy the script into your WordPress theme’s functions.php file or a custom plugin.
  2. Test Accessibility: Use screen readers and accessibility tools like Lighthouse or Axe to verify the ReCaptcha field now has an associated label.
  3. Monitor Performance: Check that the script runs correctly without interfering with LiteSpeed Cache or other optimizations.

Conclusion

This approach resolves the accessibility issue while maintaining compatibility with modern optimization tools like LiteSpeed.

It ensures your forms are both user-friendly and compliant with accessibility standards.

If you have any questions or need further assistance, feel free to reach out!

Please Login to Comment.