Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save MattRyanCo/a52f865299d90147587c8c29e3fa0553 to your computer and use it in GitHub Desktop.
Save MattRyanCo/a52f865299d90147587c8c29e3fa0553 to your computer and use it in GitHub Desktop.
Tweak Gravity Forms website field validation to insert protocol if missing (assumes http) - gets around full validation error
// Tweak Gravity Forms website field validator to insert protocol if missing (assumes http)
// Ref: https://www.itsupportguides.com/knowledge-base/gravity-forms/gravity-forms-how-to-automatically-add-http-to-submitted-website-field-values-before-validation/
add_filter( 'gform_pre_render', 'itsg_check_website_field_value' );
add_filter( 'gform_pre_validation', 'itsg_check_website_field_value' );
function itsg_check_website_field_value( $form ) {
foreach ( $form['fields'] as &$field ) { // for all form fields
if ( 'website' == $field['type'] || ( isset( $field['inputType'] ) && 'website' == $field['inputType']) ) { // select the fields that are 'website' type
$value = RGFormsModel::get_field_value($field); // get the value of the field
if (! empty($value) ) { // if value not empty
$field_id = $field['id']; // get the field id
if (! preg_match("~^(?:f|ht)tps?://~i", $value) ) { // if value does not start with ftp:// http:// or https://
$value = "http://" . $value; // add http:// to start of value
}
$_POST['input_' . $field_id] = $value; // update post with new value
}
}
}
return $form;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment