Allow "www." in the Website Field by accepting URLs that start with "www" even when users omit the protocol. When creating contact forms or user-input forms on websites, it’s common to include a field for users to enter their website URL. Many form plugins, including Gravity Forms for WordPress, validate this website field to ensure users enter a properly formatted URL. However, this validation can sometimes cause issues for users who enter URLs beginning with “www” without specifying the full protocol (e.g., "https://").
This article explains how to customize Gravity Forms’ website field validation to allow URLs starting with “www.” without requiring the “https://” prefix. You’ll also find step-by-step instructions and code snippets that you can add to your WordPress child theme or via a plugin like Code Snippets.
Customizing Validation for “www.” URLs
The good news is that Gravity Forms provides hooks and filters that allow developers to customize validation rules. You can add a filter that intercepts the website field validation process and accepts URLs starting with “www.” by automatically prepending the “https://” protocol internally before validating.
Here is the exact code snippet you can use:
add_filter( 'gform_field_validation', function( $result, $value, $form, $field ) {
if ( $field->type !== 'website' ) {
return $result;
}
if ( ! $result['is_valid'] && preg_match( '/^www\./i', trim( $value ) ) ) {
$url_with_protocol = 'https://' . trim( $value );
if ( GFCommon::is_valid_url( $url_with_protocol ) ) {
$result['is_valid'] = true;
$result['message'] = '';
}
}
return $result;
}, 10, 4 );
How This Code Works
- The filter hooks into gform_field_validation, which runs during the validation of each form field.
- It checks if the field is of type website.
- If the validation failed ($result['is_valid'] is false) but the input starts with “www.”, it prepends “https://”.
- Using GFCommon::is_valid_url(), it validates the updated URL.
- If valid, it sets the validation result to true and clears any error message.
This approach ensures that users entering URLs like www.example.com will pass validation without needing to type the full protocol.
How to Implement This in Your Website
Method 1: Add to Child Theme’s functions.php File
If you have access to your child theme’s files, add the above code snippet at the end of your functions.php file. Make sure to back up your site before editing theme files.
Method 2: Use a Code Snippets Plugin
For a safer and more manageable approach, use a WordPress plugin such as Code Snippets:
- Install and activate Code Snippets.
- Go to Snippets > Add New in your WordPress admin panel.
- Paste the code snippet into the editor.
- Save and activate the snippet.
This method avoids touching theme files and makes it easy to manage custom PHP code.
Frequently Asked Questions (FAQ)
Q1: Why does Gravity Forms require URLs to start with “https://”?
By default, Gravity Forms validates URLs strictly to ensure proper formatting and security protocols.
Q2: Can I allow other URL formats besides “www.”?
Yes, you can customize validation further using similar filter hooks depending on your needs.
Q3: Will this code affect other types of fields?
No, this code only targets fields of type “website” in Gravity Forms.
Q4: What if users enter invalid URLs starting with “www.”?
The code still validates URLs using Gravity Forms’ internal function, so invalid URLs will be rejected.
Conclusion
Allowing users to enter URLs starting with “www.” in your Gravity Forms website fields improves user experience and reduces unnecessary validation errors. By adding a simple filter hook in your child theme or using a Code Snippets plugin, you can easily modify Gravity Forms’ default behavior while keeping URL validation robust.
Feel free to reach out if you encounter any issues or want help with other customization!

Top comments (0)