Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save faisalahammad/0594adbdc940c86b3f604191c709b84f to your computer and use it in GitHub Desktop.

Select an option

Save faisalahammad/0594adbdc940c86b3f604191c709b84f to your computer and use it in GitHub Desktop.
This simple PHP snippet allows users to enter URLs starting with "www." in the website field of Gravity Forms without requiring "https://". It modifies the validation logic to accept "www." by automatically prepending "https://" for validation. Ideal for WordPress developers looking to customize form URL validation.
<?php
/**
* Allow URLs starting with "www." in Gravity Forms website field validation.
*
* This snippet enables users to enter URLs like "www.example.com"
* without needing to type "https://". It prepends "https://" internally
* for validation purposes.
*
* @author Faisal Ahammad <me@faisalahammad.com>
*/
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 );
@faisalahammad
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment